Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2005
    Location
    Oxfordshire, UK
    Posts
    144
    Thanks
    0
    Thanked 0 Times in 0 Posts

    dynamic list box

    hi, i want to add a list box to a form so that users can select a category. The categories are held in a database with two fields, catid, and category.

    How can i populate the list so users select the category name, but when it enters the choice it enters the catid...?

    Thanks in Advance.

  • #2
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Basically all you need to do is a while() statement in PHP t o go through the categories and output them in the form of a list. Maybe something like:


    PHP Code:
    // MySQL connection a and query above...

    $num mysql_num_rows($result);

    echo 
    "<select name=\"category\">\n";

    for(
    $i=0$i<=$num$i++) {

      
    $catid mysql_result($result$i"catid");
      
    $category mysql_result($result$i"category");

      if(isset(
    $catid) && isset($category)) {
        echo 
    "<option value=\"" $catid "\"> " $category ."\n";
      }

    }

    echo 
    "</select>"
    Last edited by Element; 01-10-2006 at 05:43 PM.

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    // Connect to MySQL
    $link mysql_connect('localhost''username''password');

    // Select DB
    mysql_select_db('db');

    // Retrieve category data
    $query mysql_query('SELECT * FROM categories');

    // Select box preamble
    $select '<select name="category">' "\n";

    // Populate options
    while ($row mysql_fetch_assoc($query) {
         
    $select .= '<option value="' $row['cat_id'] . '">' $row['cat_name'] . '</option>' "\n";
    }

    // Close select tag
    $select .= '</select>'  "\n";

    echo 
    $select// Print select box
    ?>
    Now select is populated with the categories, and the value of each category option will be its id.

    Edit: Aha, Element beat me to the punch while I was writing the code. Either will work.
    "$question = ( to() ) ? be() : ~be();"

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Velox Letum
    Aha, Element beat me to the punch while I was writing the code. Either will work.
    Haha, speed typing, baby.

    I would probably use Velox code. Mine may run slower. Velox will print the select values all at once, where as depending on where it is int he document and their connection a user might not be able to even use the select box until each category is finnished loading.


  •  

    Posting Permissions

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