Page 1 of 1

<SELECT> Menu with mysql options

Posted: Fri May 19, 2006 1:48 pm
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.

Posted: Fri May 19, 2006 1:56 pm
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>

Posted: Fri May 19, 2006 2:11 pm
by NiGHTFiRE
Okey thanks, i'll try it out.

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

EDIT: Figured it out.

Posted: Fri May 19, 2006 2:32 pm
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?

Posted: Fri May 19, 2006 2:46 pm
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.

Posted: Fri May 19, 2006 2:58 pm
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?

Re: <SELECT> Menu with mysql options

Posted: Fri May 19, 2006 3:00 pm
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?

Posted: Fri May 19, 2006 3:05 pm
by andym01480
misunderstood your second question nightfire! Still not sure what you want to do though.

Could you clarify further please?

Posted: Fri May 19, 2006 3:18 pm
by NiGHTFiRE
Everah: Yes that's exactly what I want.

Posted: Fri May 19, 2006 3:21 pm
by feyd
read CoderGoblin's tutorial availble in our (this server's) tutorial section.

Posted: Fri May 19, 2006 3:26 pm
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 ?

Posted: Fri May 19, 2006 3:28 pm
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>';
?>

Posted: Fri May 19, 2006 3:32 pm
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

Posted: Fri May 19, 2006 3:47 pm
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 />';
}
?>