Thursday, May 10, 2012

Populating a Drop-Down list using PHP and MySQL

In web based applications you may need to develop a drop-down list using elements taken from a database table. This code will help you to easily populate a drop-down list from a field of a database table.

 function get_dropdown($user){
         connect_to_db();
                if (!mysql_select_db('cars'))
                {
                    echo "could not select ('db')";
                }
                            $add_query = "SELECT names
                           FROM cars 
                             WHERE owner = '$user'
                            ";
                    $res = mysql_query($add_query);
               <select name="car" class="dropdownmenus" value="Select me">
     <option selected> Select Car </option>
     <?
           while ($row = mysql_fetch_array($res)) {
         extract($row);
           echo "<option value='$row'>$row[names]</option>";
           }
     ?>
     </select>
     }

No comments:

Post a Comment