nickelus Posted July 24, 2009 Share Posted July 24, 2009 i have a table containing my list i would like the name column to be dynamically displayed and the id column to be the actual post data Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/ Share on other sites More sharing options...
nickelus Posted July 24, 2009 Author Share Posted July 24, 2009 here is what i've tried, however unsuccessful i get the list to display from the query just fine but no options in my form when i echo there <?php include"bio.php";mysql_select_db($database_bio); $truck_list="SELECT vehicle_name FROM fleet"; $qry_truck=mysql_query($truck_list); while($row=mysql_fetch_array($qry_truck)){ foreach($row as $truck){ $opt="<option value='".$truck."'>".$truck."</option><br/>"; print_r($opt); }} ?> <html> <body> <div align="center"> <form action="dispatch.php" method="post" name="trucks"> <select name="trucklist"> <?php echo $opt;?> </select> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/#findComment-882155 Share on other sites More sharing options...
Raine Dragon Posted July 24, 2009 Share Posted July 24, 2009 Do you get no options at all, or are you just getting one option tag generated? Because, unless I'm mistaken, it looks like you are overwriting $opt every time you go through the foreach loop. does changing $opt="<option value='".$truck."'>".$truck."</option><br/>"; to $opt=$opt . "<option value='".$truck."'>".$truck."</option><br/>"; fix the issue? Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/#findComment-882171 Share on other sites More sharing options...
nickelus Posted July 24, 2009 Author Share Posted July 24, 2009 yes thanks, now to make it interesting I'm going to try to query two fields display one(truck_name) and post the other(truck_ID) Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/#findComment-882217 Share on other sites More sharing options...
nickelus Posted July 24, 2009 Author Share Posted July 24, 2009 OK how would i get the two columns that i queried separated to do this ? Is it by passing each column to an array in independent foreach loops then using each var in the value and display respectively Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/#findComment-882358 Share on other sites More sharing options...
lonewolf217 Posted July 24, 2009 Share Posted July 24, 2009 I threw this together, but I think it will work. you basically grab both the vehicle name and the row ID from the database. then you dynamically form the SELECT list using the ID as the value and the vehicle name as the display. when the form is submitted it will send the ID as the $_POST value <html> <body> <div align="center"> <form action="dispatch.php" method="post" name="trucks"> <select name="trucklist"> <?php include 'bio.php'; mysql_select_db($database_bio); truck_list = "SELECT vehicle_name,id FROM fleet"; $qry_truck=mysql_query($truck_list); while($row=mysql_fetch_array($qry_truck)) { echo "<OPTION value=\"" . $row['id'] . \"">" . $row['vehicle_name'] . "</OPTION>"; } ?> </SELECT> </form> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/167289-how-is-it-done-dynamic-option-list/#findComment-882371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.