Jump to content

dynamic pulldown populated by database


wkilc

Recommended Posts

This works....

 

<form name="form" action="index.php" method="get">
<?

$result = @mysql_query("select * from mytable");
if (mysql_num_rows($result) > 0) {
  print "<select name=\"instrument\">"; 
  while ($row = mysql_fetch_array($result)) {
    print "<option value=\"" . $row['instrument'] . "\">" . $row['instrument'] . "</option>\n";
  }
  print "</select>";
}
mysql_free_result($result);  

?>
<input type="submit" value="Go">
</form>

 

But I only want unique rows listed :

flute

drum

guitar

 

and not:

flute

drum

guitar

flute

 

...and I'd like them in alphabetical order (order by?)

 

I was reading about "distinct"... figured I change the query from:

$result = @mysql_query("select * from mytable");

to:

$result = @mysql_query("select distinct from mytable");

 

But that creates an error.

 

Thank you.

 

~Wayne

 

 

Link to comment
https://forums.phpfreaks.com/topic/111441-dynamic-pulldown-populated-by-database/
Share on other sites

Now I'm trying to echo "selected" so the menu holds it's current choice value... I'm pretty sure this is where I'm going wrong, syntax error:

if($_GET['instrument'] == ". $row['instrument'] ."){ echo "selected=\"selected\""; } 

 

unexpected T_ENCAPSED_AND_WHITESPACE

 

<form name="form" action="index.php" method="get">
<?

$result = @mysql_query("select distinct instrument from bocj ORDER BY instrument ASC");
if (mysql_num_rows($result) > 0) {
  print "<select name=\"instrument\">"; 
  while ($row = mysql_fetch_array($result)) {
    print "<option ";
    if($_GET['instrument'] == ". $row['instrument'] ."){ echo "selected=\"selected\""; }  
    print " value=\"" . $row['instrument'] . "\">" . $row['instrument'] . "</option>\n";
  }
  print "</select>";
}
mysql_free_result($result);  

?>
 <input type="submit" value="Go">
</form>

 

~Wayne

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.