wkilc Posted June 23, 2008 Share Posted June 23, 2008 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 More sharing options...
xtopolis Posted June 23, 2008 Share Posted June 23, 2008 SELECT DISTINCT * FROM mytable ORDER BY instrument ASC Or use DESC for descending. Try that. Link to comment https://forums.phpfreaks.com/topic/111441-dynamic-pulldown-populated-by-database/#findComment-572041 Share on other sites More sharing options...
wkilc Posted June 23, 2008 Author Share Posted June 23, 2008 Thank you. It's alphabetized now... but I'm still getting two identical "flutes" in the menu. This seems to work: select distinct instrument from bocj ORDER BY instrument ASC Thanks again! ~Wayne Link to comment https://forums.phpfreaks.com/topic/111441-dynamic-pulldown-populated-by-database/#findComment-572134 Share on other sites More sharing options...
wkilc Posted June 23, 2008 Author Share Posted June 23, 2008 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 Link to comment https://forums.phpfreaks.com/topic/111441-dynamic-pulldown-populated-by-database/#findComment-572141 Share on other sites More sharing options...
xtopolis Posted June 23, 2008 Share Posted June 23, 2008 if($_GET['instrument'] == ". $row['instrument'] ."){ echo "selected=\"selected\""; } to if($_GET['instrument'] == $row['instrument']){ echo "selected=\"selected\""; } No double quotes + . around the $row['instrument'] Link to comment https://forums.phpfreaks.com/topic/111441-dynamic-pulldown-populated-by-database/#findComment-572565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.