Populating php dropdown menu with mysql results

I am trying to populate a drop down menu with php/mysql. Wouldn’t this be the typical code for something like that? Thanks for any reviews and suggestions for code that actually works. I can’t get this one to work at all.

[php]<?php

     $query="SELECT whatever from tablename";
     $result=mysql_query($query);
     while($row=mysql_fetch_array($result,MYSQL_ASSOC))

     $whateverid = $row['whateverid'];
         $whatevername = $row['whatevername'];

{
echo ‘’ . $row[‘whatevername’]. ‘’;
}
echo ‘’;

?>[/php]

[php]

<?php $query="SELECT whatever from tablename"; $result=mysql_query($query); echo ''; // i iadded this line while($row=mysql_fetch_array($result,MYSQL_ASSOC)) { // $whateverid = $row['whateverid']; // $whatevername = $row['whatevername']; i dont think u need these those vars echo '' . $row['whatevername']. ''; } echo ''; ?>

[/php]

Hi, i would do this like this
[php]
$query=“SELECT whatever from tablename”;
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result)) {

       $whateverid = $row['whateverid'];
       $whatevername = $row['whatevername'];
   # the .= appends to the string so if there is more than one option they will  all be added to the variable drop down    
  $dropdown .= '<option value="' . $row['whateverid'] . '">' . $row['whatevername']. '</option>';

  }

display the drop down menu with the options

echo ’

‘.$dropdown.’
';

?>
[/php]

yeap that would work fine.

i will mark this thread solved.

Would you mind helping me with some proper syntax if I wanted to link each item in the dropdown with a hyperlink? Tried several times but don’t quite know where to put the <a href… with the loop happening. Thanks.

And thank you very much!

like this
[php]

Google [/php]

This code properly displays the table contents, thank you. However, it won’t send the browser to the corresponding hyperlink when the item is selected from the dropdown menu. Thoughts? Thanks.

[php]<?php
$query=“SELECT * from mytable”;
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{

       $typeid = $row['typeid'];
       $foodtype = $row['foodtype'];

// the .= appends to the string to account for more than one variable listed in drop down

$dropdown .= ‘<option value=“index.php?content=buyproducts&id=$id”’ . $row[‘id’] . ‘">’ . $row[‘name’]. ‘’;
}

// display the drop down menu with the options
echo ‘’.$dropdown.’’;

?>[/php]

you need to trigger the selectedindex change using javascript.

take a look at the followig link
https://www.google.com/webhp?sourceid=chrome-instant&ie=UTF-8#hl=en&tbo=d&output=search&sclient=psy-ab&q=javascript%20selectedindex&oq=&gs_l=&pbx=1&fp=1d332d38cdc82502&bpcl=38625945&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&biw=1920&bih=955

im not good at javascript.

good luck

This is quite simple with jQuery. Here is an example:

<html>
<head>
	<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {
		$('select[name="myOptions"]').change(function() {
			if ($(this).val() !== '') {
				location.href = $(this).val();
			}
		});
	});
	</script>
</head>
<body>
<select name="myOptions">
	<option value="">Select Option</option>
	<option value="http://www.yahoo.com/">Yahoo</option>
	<option value="http://www.google.com/">Google</option>
</select>
</body>
</html>

I have tried several of these types of scripts and codes. I’m sure they all work but I don’t know how to get the ‘on click’ to work or ‘on select’ to work with a hyperlink. At this point I don’t even mind if I have to select from the drop down and then click ‘go’. Anyone care to help with that? At this point I’d like to have the code I shared above be marked to demonstrate how to add a hyperlink to each ‘foreach’ item. Follow? I have placed the link several places in the code and tested but nothing works.

Each hyperlink will be different based on the dropdown menu item selected. How do I make that correspond? My code is here in this thread. Feel free to combine my code to produce an example if you’d like. I’d like to learn how to do this but after two days of search engines and forums, I’m stuck. Thanks.

Thanks again for your help. I appreciate it.

Just combining your code with my example:

[php]

<?php $dropdown = ''; // empty string should be specified before appending to it $query = "SELECT * from mytable"; $result = mysql_query($query); while($row = mysql_fetch_assoc($result)) { $dropdown .= '' . $row['name']. ''; } ?> <?php echo $dropdown; ?> [/php]
Sponsor our Newsletter | Privacy Policy | Terms of Service