drop down list from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

drop down list from mysql

Post by lmhart »

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

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>';
 
?>
 
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

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 
 
 
?>
 
User avatar
Weiry
Forum Contributor
Posts: 323
Joined: Wed Sep 09, 2009 5:55 am
Location: Australia

Re: drop down list from mysql

Post by Weiry »

basically, you are on the right track with populating a dropdown menu.

There are a few problems in the code you have at the moment.
Im not entirely sure how you have your connection to the database setup in dbc.php
But im going to assume the connection is stored in a variabled named $connection, its important to use that connection variable when running a query, so the query will use the connection that you have set up.

Code: Select all

 
<?php
include 'dbc.php';
 
$query = "SELECT * FROM `states`";
$result = mysql_query($query,$connection);
 
print "<select name='student'>";
print "<option>Student Name</option>";
 
while($nt = mysql_fetch_array($result)){
    print "<option value='{$nt['id']}'>{$nt['name']}</option>";
}
print "</select>";
?>
 
For starters

Code: Select all

echo "<select name=student value=''>Student Name</option>";
This line does not make any sense syntax wise. You open a select tag, fair enough, you then close an option tag without opening one first. I dont know if this in fact does work because i always use full tags for readability. But also, in your select tag, you should encase student with ' '.

Code: Select all

print "<select name='student'>";
print "<option>Student Name</option>";

Code: Select all

echo "<option value=$nt[id]>$nt[name]</option>";
i inserted a few things here, so the code would recognise them as string variables.
but also, you did not encase $nt[id] with ' ' which could cause problems.

Code: Select all

print "<option value='{$nt['id']}'>{$nt['name']}</option>";
lmhart
Forum Newbie
Posts: 15
Joined: Tue Apr 14, 2009 1:05 pm

Re: drop down list from mysql

Post by lmhart »

Thanks all for the help.
Post Reply