<SELECT> Menu with mysql options
Moderator: General Moderators
<SELECT> Menu with mysql options
Hey,
Is there some way you can create the <select> menu with options from a database.
Example:
I've got a column with highfive as the data. Then i want an option in the <select> menu to be highfive, example:
<select name="test">
<OPTION VALUE="highfive">highfive</OPTION>
</select>
Is there some way to do like that?
Thanks.
Is there some way you can create the <select> menu with options from a database.
Example:
I've got a column with highfive as the data. Then i want an option in the <select> menu to be highfive, example:
<select name="test">
<OPTION VALUE="highfive">highfive</OPTION>
</select>
Is there some way to do like that?
Thanks.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
Did that recently for a site. Here's what I used, change the db variables etc for your application
Code: Select all
$choosebreed=mysql_query("SELECT name FROM breed ORDER BY name") or die("Query failed");
echo"<form name=\"form1\">";
echo"<select name=\"site1\" size=1 >";
echo"<option value=\"\">Breeds....</option>";
while ($newrow=mysql_fetch_row($choosebreed)){
echo "<option value=\"breed.php?breed=$newrow[0]\">$newrow[0]</option>";
}
?>
</select>
</form>- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
Try it as is and then look at the result in a browser and view source.
What happens is that the query grabs a bunch of results that fits your criteria and put them in an array result that I called $choosebreed.
What the line
does is grab a result into a new array called $newrow. Now in my application the query only looked for one field of the database, so each time round the while loop there was only a value called $newrow[0].
The while loop goes through the $choosebreed array result for every result that was grabbed from the database.
There would only be a $newrow[1] if you had looked for 2 fields from the database like
in that case each iteration of the while loop would have $newrow[0] as a name and $newrow[1] as an age.
In between the {} of the while statement you would deal with that particular instance of $newrow[0] and $newrow[1], which would then be different on the next iteration.
Hope that makes sense.
What happens is that the query grabs a bunch of results that fits your criteria and put them in an array result that I called $choosebreed.
What the line
Code: Select all
while ($newrow=mysql_fetch_row($choosebreed)){The while loop goes through the $choosebreed array result for every result that was grabbed from the database.
There would only be a $newrow[1] if you had looked for 2 fields from the database like
Code: Select all
$query="SELECT name,age FROM table";In between the {} of the while statement you would deal with that particular instance of $newrow[0] and $newrow[1], which would then be different on the next iteration.
Hope that makes sense.
I know all that. But then if it got $newrow[1] and i want to get stuff out of the database and i got ids for each row would i say "SELECT * FROM $table_name WHERE id=2"; or would i go with 1 there as well?
Last edited by NiGHTFiRE on Fri May 19, 2006 3:00 pm, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: <SELECT> Menu with mysql options
Are you talking about a query that returns data from the database and builds a select option list?NiGHTFiRE wrote:Hey,
Is there some way you can create the <select> menu with options from a database.
Example:
I've got a column with highfive as the data. Then i want an option in the <select> menu to be highfive, example:
<select name="test">
<OPTION VALUE="highfive">highfive</OPTION>
</select>
Is there some way to do like that?
Thanks.
- andym01480
- Forum Contributor
- Posts: 390
- Joined: Wed Apr 19, 2006 5:01 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This is the link to CoderGoblin's tutorial. I am not sure that is exactly what you want though. Try something along the lines of (replacing your setting for the ones in this snippet) ...
Code: Select all
<?php
$sql = "SELECT item_id, item_name
FROM items
ORDER BY item_id ASC";
if (!$result = mysql_query($sql))
{
die("Could not get the item list: " . mysql_error());
}
echo '<form method="post" action="resultpage.php">';
echo '<select name="myitems">';
while ($row = mysql_fetch_array($result))
{
echo '<option value="' . $row['item_id'] . '">' . $row['item_name'] . '</option>';
}
echo '</select>';
echo '</form>';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Pass the item_id to the next page. It will be part of the $_POST array. Something like...
Code: Select all
<?php
if (isset($_POST['myitems']))
{
// You would really want to validate this here,
// But I am keeping this intentionally simple
$item_id = $_POST['myitems'];
$sql = "SELECT *
FROM items
WHERE item_id = $item_id";
if (!$result = mysql_query($sql))
{
die("Could not get the selected item: " . mysql_error());
}
$item_array = mysql_fetch_array($result);
// Now you can use the table field names as keys to the item_array
echo 'You chose item id ' . $item_array['item_id'] . '.<br />';
echo 'This item is described as ' . $item_array['item_description'] . '.<br />';
}
?>