我有两个表“ oc_t_item_car_make_attr ”,“ oc_t_item_car_model_attr ”
那么我怎样才能从这两个表中创建一个可靠的下拉列表呢?
实际上没有很好地理解问题,但你可能想要这样的东西:
SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.fk_i_make_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id = ?
您需要在问号的位置插入 id。
Here is the entire code what your looking for:
Below code will display all car names from database dynamically:
<select name="cars" id="carmodels">
<option value="">select</option>
<?php
$qry = mysqli_query("select * from oc_t_item_car_make_attr");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>
</select>
Below result will display car models dependent on the names selected above:
html:
<select name="models" id="models">
</select>
you need to use ajax to get the car models from database:
$(document).on('change','#carmodels',function(){
var value = $(this).val();
$.ajax({
url: "demo.php",
type: "GET",
data: { id : value},
success: function(data){
$("#models").html(data);
}
});
demo.php:
<?php
include "connection.php"; //database connection code
$qry = mysql_query("SELECT * FROM oc_t_item_car_make_attr
INNER JOIN oc_t_item_car_model_attr
ON oc_t_item_car_model_attr.pk_i_id = oc_t_item_car_make_attr.pk_i_id
WHERE oc_t_item_car_make_attr.pk_i_id =".$_GET['id'].");
while($row=mysqli_fetch_array($qry)){
echo "<option value=".$row['pk_i_id'].">".$row['s_name']."</option>";
}
?>