SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Location
    Chicago, IL
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating a <select> field from a db.

    Hello all.

    I have a mysql db, with a table that contains a set of values. My question is how would I loop through each one of the values in that set, to populate a <select> tag in PHP? I know this can't be too hard, but I can't figure it out.

    Thanks

  2. #2
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Grand Rapids, MI
    Posts
    1,284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    // Connect to db and all that stuff first

    $getData mysql_query("SELECT * FROM blah");
    ?>
    <select name="products">
    <?php
    while ($values mysql_fetch_array($getData)) {
       
    $name $values['Name'// Note: The Name in '' is a column name from the db
       
    $id $values['ID'// Same as above
       
    echo "<option value=\"$id\">$name</option>";
    }

    ?>
    </select>
    Of couse you should name the select tag something useful and such, but thats it. If this is on a form to process it you just do:
    PHP Code:
    <?php

    // Submitted form data:
    $id $_POST['products'];

    ?>
    $id holds the ID of the selected product.

  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Location
    Chicago, IL
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey Ben,

    Thanks a lot. This works great. Had to play around with the database structure a bit, but in the end it worked.

    Thanks again.


Bookmarks

Posting Permissions

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