954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

popluating a drop down menu from the database

hi i'm having trouble creating a drop down menu using data from the database and i dont understand where the problem is

<?php
....
$conn ...blah blah blah...
mysql_select_db ...blah blah blah...
....

$query = mysql_query("SELECT * FROM teamdb WHERE sports='Basketball'");
$options = "";
	
while($row = mysql_fetch_array($query))
{
	$teamid = $row["teamid"];
	$teamname = $row["teamname"];
	$options.="<option value=\"$teamid\">".$teamname."</option>";
}

....
?>
<html>
....

<select name='whatever'>
<option value=0> choose</option>
<?=$options?>
</select>

....
</html>


the drop down menu created from this code only has the choose option
and i dont understand whats wrong with it
can anybody help?

and after that is fixed
if i want to put that while loop into a function so i choose which sport to create the menu for.
i just need :
function selectteam ($where)
put the query ... while loop into the function
and change the WHERE field to '$where'
right?

thanks for any help in advance

blahbla
Newbie Poster
24 posts since Sep 2009
Reputation Points: 11
Solved Threads: 1
 

Firstly, to correct your script:

<?php
....
$conn ...blah blah blah...
mysql_select_db ...blah blah blah...
....

$query = mysql_query("SELECT * FROM teamdb WHERE sports='Basketball'");

....
?>
<html>
....

<select name='whatever'>
<?php
while($row = mysql_fetch_array($query))
{
echo '<option value"='.$row['teamid'].'">'.$row['teamname'].'</option>';	

}
?>
</select>

....
</html>
simplypixie
Posting Pro in Training
447 posts since Oct 2010
Reputation Points: 116
Solved Threads: 82
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You