<SELECT> Menu with mysql options

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
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

<SELECT> Menu with mysql options

Post by NiGHTFiRE »

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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

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>
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Okey thanks, i'll try it out.

EDIT: What should i insert intead of Breeds in the name.

EDIT: Figured it out.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Now another question. How would i insert what should happen. I mean, if
<option value=\"visabud.php?auktioner=$newrow[0]\">$newrow[0]</option>
goes to $newrow[1] , how would i then choose what to display?
what i do like if(isset = $newrow[0])) { ? or what?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

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

Code: Select all

while ($newrow=mysql_fetch_row($choosebreed)){
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

Code: Select all

$query="SELECT name,age FROM table";
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.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: <SELECT> Menu with mysql options

Post by RobertGonzalez »

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.
Are you talking about a query that returns data from the database and builds a select option list?
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

misunderstood your second question nightfire! Still not sure what you want to do though.

Could you clarify further please?
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Everah: Yes that's exactly what I want.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read CoderGoblin's tutorial availble in our (this server's) tutorial section.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

feyd wrote:read CoderGoblin's tutorial availble in our (this server's) tutorial section.
I don't know any javascript so it's kinda hard and i use MySQL. Isn't there just any way to make a while loop and just add options ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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>';
?>
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Everah: That's what I need. Now another question.
Now if he wants anything and then presses submit I want him to see everything on that mysql row. But i don't know how I would do that.
Tell me if you don't understand.
I'm bad at explaining

EDIT: Think i know, i'll try it tomorrow
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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 />';
}
?>
Post Reply