jlock
jlock

Reputation: 69

How To create a Dependable Dropdown From Two Tables

This is my first table

This is my second table

I have two tables "oc_t_item_car_make_attr" , "oc_t_item_car_model_attr"

So how can i make a Dependable Dropdown From these Two Tables?

Upvotes: 1

Views: 312

Answers (4)

w3b
w3b

Reputation: 853

You can only do this by Ajax.

Following is city country reference site, you can get idea from that.

Referance Site

if you stuck any where feel free to ask me.

Upvotes: 1

user2906577
user2906577

Reputation:

Hey you can do this by ajax

Reference Link 1

Reference Link 2

also study inner join

Upvotes: 1

Joke_Sense10
Joke_Sense10

Reputation: 5402

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>";
}
?>

Upvotes: 1

ST3
ST3

Reputation: 8946

Actually didn't understand question very well, but you probably want something like this:

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 = ?

You need to insert id in the place of question mark.

Upvotes: 1

Related Questions