Results 1 to 5 of 5

Thread: PHP Listbox

  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Listbox

    I don't know if PHP gets discussed on this site, but I will try. I have a query that works at the moment, filling a dropdown list. However I don't want it as a dropdown. Is it possible to fill a normally open listbox, that shows 15 or so rows, with a scrollbar to scroll down?

    Many thanks

    My query at present is:

    // Get all the data from the "TXMASTERS" table
    $result = mysql_query("SELECT Barcode FROM TXMASTERS Order by Barcode")
    or die(mysql_error());

    echo '<Select Barcode="Barcode">';
    while($nt=mysql_fetch_array($result)) { // Array or records stored in nt




    echo '<option value="'.$nt[Barcode].'">' .$nt['Barcode'].'</option>';
    }
    echo '</select>'; // closing list

    ?>

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: PHP Listbox

    In your <select> tag, add a size attribute, e.g., <code>size="15"</code> to have a 15-row box.

    <code>echo '<Select Barcode="Barcode" size="15">';</code>

    (Usually controls have a name and/or id attribute, too. Not sure why you don't have that.)

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: PHP Listbox

    Many thanks, very greatful. I have been trying to do this from last Friday!!

    Re your remarks on the name etc, I have been struggling with this code from bits found on the internet.
    I know somewhere along the line I will need to pass an ID value as well as the chosen item, or whether like in Access you can have columns with one carrying the value on click

    The rest of my code is:

    // Get all the data from the "TXMASTERS" table
    $result = mysql_query("SELECT Barcode FROM TXMASTERS Order by Barcode")
    or die(mysql_error());

    echo '<Select Barcode="Barcode" size="15">';

    while($nt=mysql_fetch_array($result)) { // Array or records stored in nt

    echo '<option value="'.$nt[Barcode].'">' .$nt['Barcode'].'</option>';
    }
    echo '</select>'; // closing list

    ?>

    </td>
    </tr>
    <tr>
    <td><input type="submit" value="Submit"> </td>
    </tr>
    </table>
    </form>

    </body>

    </html>

    So I don't know what is missing, best practice. In addition it would be good to know if I can change the listbox width, and if I have any control over where it is on my page. Many thanks again

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: PHP Listbox

    <P ID="edit" class=small>(Edited by jscher2000 on 14-Jul-08 13:37. )</P>
    <hr>So I don't know what is missing, best practice.<hr>
    Try changing <code>Barcode="Barcode"</code> to <code>name="Barcode"</code>. When your form is submitted, this will help in identifying the selected option -- it will be the post variable named "Barcode".

    <hr>In addition it would be good to know if I can change the listbox width, and if I have any control over where it is on my page.<hr>
    You can style a <select> using CSS. That is not a PHP issue, so you can refer to any CSS reference materials on the web. You can just slide it into your <select> tag, for example:

    <code>echo '<select name="Barcode" size="15" style="width:50px;">';</code>

    With respect to position, your listbox is in a table cell. The traditional row-and-column layout of a table can be a bit limiting, but this, too, is not a PHP issue. Your PHP code inserts query results into an HTML structure and delivers pure HTML to the browser. For more assistance in creating HTML tables, or forms without tables, you have a web full of reference materials. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  5. #5
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: PHP Listbox

    Many, many thanks. Explained to me very well, at last I can start moving. All the best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •