Jump to content

how is it done: dynamic option list


nickelus

Recommended Posts

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>

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?

 

 

 

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>

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.