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 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help needed with PHP/Mysql dropdown list

    I have this function that generate dropdown from mysql.

    I want to midify this for update form (Ie i want to display the selected item first and the other choices after that.)

    How can i do this.

    Thanks.




    Code:
    function dropdown()
    {
    // open connection 
    
    $con = mysql_connect('test', 'user', 'pass');
    // select database 
    mysql_select_db("test", $con);
    
    //this code is bringing in the values for the dropdown. 
    $query="select buildingId,Name from buildings order by Name"; 
    
    /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ 
    
    $result = mysql_query ($query); 
    while($row=mysql_fetch_array($result)){//Array or records stored 
    echo "<option value=$row[buildingId]>$row[Name]</option>"; 
    /* Option values are added by looping through the array */ 
    } 
    echo "</select>";// Closing of list box 
    }
    ?>

  • #2
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    pass in a default value to this function..
    example:
    if you want the drop down with value1 selected
    dropdown("value1");

    PHP Code:
    <?php
    function dropdown($default_value null)
    {

    // open connection 
    $con mysql_connect('test''user''pass');
    // select database 
    mysql_select_db("test"$con);

    //this code is bringing in the values for the dropdown. 
    $query="select buildingId,Name from buildings order by Name"

    /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */ 

    $result mysql_query ($query); 
    while(
    $row=mysql_fetch_array($result)){//Array or records stored 
    if($row[buildingId] == $default_value)
    {
        echo 
    "<option value=$row[buildingId] selected='selected'>$row[Name]</option>"
    }
    else
    {
        echo 
    "<option value=$row[buildingId]>$row[Name]</option>"
    }

    /* Option values are added by looping through the array */ 

    echo 
    "</select>";// Closing of list box 
    }
    ?>


  •  

    Posting Permissions

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