drop down list from mysql
Posted: Tue Oct 06, 2009 3:51 pm
I am needing to populate a drop down menu list from a table in a mysql database.
I managed to do it by hardcoding an array such as
But would really like to be able to store the states in a database and then just populate from there. I found some sample code and tried to make it work but I do not understand the process for populating the drop down
I managed to do it by hardcoding an array such as
Code: Select all
<?php
$category = array(
1=> "AL",
2=> "AK",
3=> "AZ",
4=> "AR",
5=> "CA",
6=> "CO",
7=> "CT",
8=> "DE",
9=> "FL",
10=> "GA"
);
$category = str_replace(" ", " ", $category);
echo '<SELECT name=category>';
foreach ($category as $key => $value)
{
echo '<OPTION value='.$value.'> '.$value.'';
}
echo '</select>';
?>
Code: Select all
<?php
include 'dbc.php';
$query="Select * from states";
$result = mysql_query ($query);
$options="";
echo "<select name=student value=''>Student Name</option>";
// printing the list box select command
while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
echo "<option value=$nt[id]>$nt[name]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>