SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist achintya's Avatar
    Join Date
    Apr 2005
    Location
    Chandannagar(India)
    Posts
    459
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    dynamic drop-down menu

    Hi all,
    how can I retrieve data from database into a drop-down menu box?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a database table with the name, let's say "test" that looks like this

    ID Name
    1 Name1
    2 Name2
    3 Name3
    4 Name4
    . .
    . .
    . .

    you can do something like this

    Code:
    $sql = "SELECT ID, Name FROM test";
       
       $result = mysql_query($sql);
        
        echo "<select name='test_names'>";
        
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $option = "<option value='".$row['ID']."'";
        $option .= (isset($_POST['test_names']) && $_POST['test_names'] == $row['ID']) ? "selected" : "";
        $option .= ">".$row['Name']."</option>";
        echo $option;   
        }
        
        echo "</select>";


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
  •