JQuery ajax unlimited dynamic selectbox based on parent categories

I have a table for the category:

| category_id | category_name | parent_id
| 1           | Electronics   | 0
| 2           | Mobile Phones | 1
| 3           | Computers     | 1
| 4           | Iphone        | 2
| 5           | Samsung Galaxy| 2
| 6           | Asus Laptop   | 3

Thus, this table can store an unlimited category of children for the parent category.

Now, what I'm trying to achieve, let's say that I select Electronics, then another selectbox will appear with a list of values Mobile Phonesand Computers. And then, if I choose Computers, another selectbox appears with a list of values Asus Laptop.

I know how to encode a dynamic selection block, but it does not work with unlimited subcategories.

This should work like this (based on my thinking).

  • User selects category from first selectbox
  • Send ajax to getcategory.php
  • getcategory.php , - .
  • ajax getcategory.php
  • , jQuery .
  • selectbox
  • ajax getcategory.php
  • getcategory.php , - .
  • ajax getcategory.php
  • , jQuery .
  • .. , selectbox .

? ? ?

+4
2

, 1) conf.php

<?php

$conn = mysqli_connect ('localhost', 'root', 'root', 'test') die (mysqli_error ($ conn));? >

2) -dropdown.php

<?php
include 'conf.php';

$query = 'SELECT * FROM category WHERE parent_id=0';

$result = mysqli_query($conn,$query);
$data = $result->fetch_all(MYSQLI_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>MultiDropdown</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-4">
<select name="category" class="form-control category">
<option value="">Select</option>
<?php
foreach($data as $d){
echo '<option value="'.$d['id'].'">'.$d['category'].'</option>';
}
?>
</select>
</div>
</div>
<div id="dropdown_container"></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">  </script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('change','.category',function(){
var id = $(this).val();
$.ajax({
url:'getcategory.php',
type:'post',
data:{'id':id},
success:function(data){
//alert(data);
$('#dropdown_container').append(data);
}
})
});
});
</script>
</body>
</html>

3) getcategory.php

<?php
include 'conf.php';
if(isset($_POST['id'])){
$id= $_POST['id'];
$query = 'SELECT * FROM category WHERE parent_id = '.$id;
$result = mysqli_query($conn,$query);
$data = $result->fetch_all(MYSQLI_ASSOC);
if(!empty($data)){
echo '<div class="row">
<div class="col-md-4">
<select name="category" class="form-control category">
<option value="">Select</option>';
foreach($data as $d){
echo '<option value="'.$d['id'].'">'.$d['category'].'</option>';
}
echo '</select>
</div>
</div>';
}
}
?>
0

-, : ( , , , )

SQL null:

SELECT * FROM category WHERE parent_id = ?

, --------------

, .

:

id     category
------ ----------
11     Electronics
1101   Mobile Phones
1102   Computers
110101 Iphone
110102 Samsung Galaxy
110201 Asus Laptop

max 99 child, , 3- ..

: , 11, :

SELECT * FROM products WHERE category_id like '11%'
0

Source: https://habr.com/ru/post/1649350/


All Articles