dynamic dropdown list
-
Hi am back again after weeks of trying to populate multiple combo box from a mysql database. I have a script that loads one combo box / drop down list well by populating car make. The lists are supposed to be first "car make" then second "car model" third "car badge" fourth is "car description" the code at present is
<?php
include("../includes/store.php"); //echo mysql command $query = "SELECT make FROM cars"; $result = mysql\_query($query) or die(mysql\_error()); //excute or return if there is an error echo "Make"; $dropdown = "<select name='make'>"; while ($row = mysql\_fetch\_assoc($result)) { $dropdown .= "\\r\\n<option value='{$row\['make'\]}'>{$row\['make'\]}</option>"; } $dropdown .= "\\r\\n</select>"; echo $dropdown; echo "<br>" . "<br>"; $query1 = "SELECT model FROM carmodel WHERE makeid = {$row\['id'\]}"; $carmodel = mysql\_query($query1) or die(mysql\_error()); //execute query and load fields to page echo "Model"; $list2 = "<select name='model'>"; while ($row = mysql\_fetch\_assoc($carmodel)) { $list2 .= "\\r\\n<option value='{$row\['model'\]}'>{$row\['model'\]}</option>"; } $list2 .= "\\r\\n</select>"; echo $list2;
?>
The database table are table 1 cars = "carid" & "make" table 2 carmodel = "modelid" & "makeid" & "model" table 3 badge = "badgeid" & "modelid" & "badge" table 4 cardesc = "descid" & "badgeid" & "yearofman" & "transmission" & "drivetype" & "fueltype" I can populate the first dropdown list but how to get the second drop down to populate with the car model records based on the selected car make then the cars badge of the filtered car model the description is proving to be a pickle. Please help as after days of searching and playing with code cant seem to nail it right. Thanks in advance.
-
Hi am back again after weeks of trying to populate multiple combo box from a mysql database. I have a script that loads one combo box / drop down list well by populating car make. The lists are supposed to be first "car make" then second "car model" third "car badge" fourth is "car description" the code at present is
<?php
include("../includes/store.php"); //echo mysql command $query = "SELECT make FROM cars"; $result = mysql\_query($query) or die(mysql\_error()); //excute or return if there is an error echo "Make"; $dropdown = "<select name='make'>"; while ($row = mysql\_fetch\_assoc($result)) { $dropdown .= "\\r\\n<option value='{$row\['make'\]}'>{$row\['make'\]}</option>"; } $dropdown .= "\\r\\n</select>"; echo $dropdown; echo "<br>" . "<br>"; $query1 = "SELECT model FROM carmodel WHERE makeid = {$row\['id'\]}"; $carmodel = mysql\_query($query1) or die(mysql\_error()); //execute query and load fields to page echo "Model"; $list2 = "<select name='model'>"; while ($row = mysql\_fetch\_assoc($carmodel)) { $list2 .= "\\r\\n<option value='{$row\['model'\]}'>{$row\['model'\]}</option>"; } $list2 .= "\\r\\n</select>"; echo $list2;
?>
The database table are table 1 cars = "carid" & "make" table 2 carmodel = "modelid" & "makeid" & "model" table 3 badge = "badgeid" & "modelid" & "badge" table 4 cardesc = "descid" & "badgeid" & "yearofman" & "transmission" & "drivetype" & "fueltype" I can populate the first dropdown list but how to get the second drop down to populate with the car model records based on the selected car make then the cars badge of the filtered car model the description is proving to be a pickle. Please help as after days of searching and playing with code cant seem to nail it right. Thanks in advance.
I'm affraid your page logic is off slightly. Your sublist (model) should be dependant on your primary list (cars). Which would mean that on a selection change on the client's end you either need to load the sub list from the server using Ajax or you need to prepare multiple sub selects and display the one belonging to the cars that is selected. If you opt for the first alternative you will need to create a seperate php file that will provide you the options for the 'model' select. You should then call this page on the 'onchange' event of the 'make' select using Ajax and fill the 'make' select. If you opt for the second alternative you will have to generate an option list for each of the various 'make' possibilities and store them in either invisible select elements or in JavaScript array's. Eg:
$query1 = "SELECT model FROM carmodel ORDER BY makeid";
$carmodel = mysql_query($query1) or die(mysql_error());
$last_make = -1;
$list2 = "";
while ($row = mysql_fetch_assoc($carmodel))
{
if ( $last_make != $row['makeid'] ) {
if ($list2 != '') $list .= "";
$list2 .= "";
$last_make = $row['makeid'];
}
$list2 .= "\r\n{$row['model']}";
}
$list2 .= "\r\n";
echo $list2;Please note I've not tested the code so it might contain some mistakes, but the overall logic should be clear.