greencoin Posted June 12, 2007 Share Posted June 12, 2007 Am having a problem with a dynamic, multiple drop down box query. If I run the code with only one select, it populates fine. If I run it with 2 selects then only the first drop down box populates - the second drop down box is empty. Am wondering if I need to put the query into an array and populate the boxes from there. Each select represents a start and end date so when the user queries, it brings back table records entered in between those ranges. Thanks in advance ~Rich ...................................................................... $query = "SELECT date FROM `GC_Tracker`"; $result = mysql_query($query) or die(mysql_error()); ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <? while(list($name)= mysql_fetch_row($result)) { ?> <option value="<? echo $name; ?>"><?php echo "$name"; ?></option> <?php } ?> </select> <select size="1" name="max"> <? while(list($name)= mysql_fetch_row($result)) { ?> <option value="<? echo $name; ?>"><?php echo "$name"; ?></option> <?php } ?> </select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/ Share on other sites More sharing options...
The Little Guy Posted June 12, 2007 Share Posted June 12, 2007 Try this: <?php $query = "SELECT date FROM `GC_Tracker`"; $result = mysql_query($query)or die(mysql_error()); ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <? while($row = mysql_fetch_row($result)) { ?> <option value="<?php echo $row['date']; ?>"><?php echo $row['date']; ?></option> <?php } ?> </select> <select size="1" name="max"> <?php while($rows = mysql_fetch_row($result)) { ?> <option value="<? echo $rows['date']; ?>"><?php echo $rows['date']; ?></option> <?php } ?> </select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273379 Share on other sites More sharing options...
greencoin Posted June 12, 2007 Author Share Posted June 12, 2007 Sorry - now both drop down boxes are empty. ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273396 Share on other sites More sharing options...
The Little Guy Posted June 12, 2007 Share Posted June 12, 2007 That is beacuse I did a small error I didn't see Try this: <?php $query = "SELECT date FROM `GC_Tracker`"; $result = mysql_query($query)or die(mysql_error()); ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <?php while($row = mysql_fetch_array($result)) { ?> <option value="<?php echo $row['date']; ?>"><?php echo $row['date']; ?></option> <?php } ?> </select> <select size="1" name="max"> <?php while($rows = mysql_fetch_array($result)) { ?> <option value="<? echo $rows['date']; ?>"><?php echo $rows['date']; ?></option> <?php } ?> </select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273402 Share on other sites More sharing options...
greencoin Posted June 12, 2007 Author Share Posted June 12, 2007 Sorry - still doesn't work... but I did get it to work with a double query... not preferred but I'll take it for now so I can move forward. here's the code; $query = "SELECT date FROM `GC_Tracker`"; $result = mysql_query($query) or die(mysql_error()); $query2 = "SELECT date FROM `GC_Tracker`"; $result2 = mysql_query($query2) or die(mysql_error()); ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <? while(list($row) = mysql_fetch_row($result)) { ?> <option value="<?php echo $row; ?>"><?php echo $row; ?></option> <?php } ?> </select> <select size="1" name="max"> <?php while(list($rows) = mysql_fetch_row($result2)) { ?> <option value="<? echo $rows; ?>"><?php echo $rows; ?></option> <?php } ?> </select><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p> </form> I'm not gonna call this solved till someone can figure out a way to do it without a double hit to mysql... ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273412 Share on other sites More sharing options...
The Little Guy Posted June 12, 2007 Share Posted June 12, 2007 to use the query more than once, you most likely need to rest the pointer using reset() So... place this after the first loop. reset($row); Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273422 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 <?php $query = 'SELECT date FROM GC_Tracker'; $result = mysql_query($query) or die(mysql_error()); $select_options = array(); if ($result && mysql_num_rows($result)) while ($row = mysql_fetch_row($result)) $select_array[] = "\t<option value=\"$row[0]\">$row[0]</option>\n"; ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <?php echo implode('',$select_options); ?> </select> <select size="1" name="max"> <?php echo implode('',$select_options); ?> </select> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273424 Share on other sites More sharing options...
greencoin Posted June 12, 2007 Author Share Posted June 12, 2007 The little guy: I put it after the first loop in the <?php reset($row) ; } ?>only the first box populates Wildbug: no good - neither drop down box gets populated I'm doing this on a Godaddy, Linux hosting plan using Mysql 4.1 and whatever limited flavor PHP they're using. Thanks ~Rich Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273442 Share on other sites More sharing options...
Wildbug Posted June 12, 2007 Share Posted June 12, 2007 Oh, I made a typo. "$select_array" -> "$select_options" on the fifth line. <?php $query = 'SELECT date FROM GC_Tracker'; $result = mysql_query($query) or die(mysql_error()); $select_options = array(); if ($result && mysql_num_rows($result)) while ($row = mysql_fetch_row($result)) $select_options[] = "\t<option value=\"$row[0]\">$row[0]</option>\n"; ?> Select<form method="POST" action="track_date_results.php"> <select size="1" name="min"> <?php echo implode('',$select_options); ?> </select> <select size="1" name="max"> <?php echo implode('',$select_options); ?> </select> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273463 Share on other sites More sharing options...
greencoin Posted June 12, 2007 Author Share Posted June 12, 2007 You Da' man Wildbug - problem solved! Thanks everyone who offered help. ~Rich P.S., I've just posted another where I get a parse error on the results page! <grin> Quote Link to comment https://forums.phpfreaks.com/topic/55300-solved-newbie-needs-help-populating-multiple-drop-down-boxes-from-mysql-table/#findComment-273465 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.