| 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
因此,该表能够为父类别存储无限的子类别.
现在我想要实现的是,让我们说我选择电子产品,然后另一个选择框应该出现值列表移动电话和计算机.
然后,如果我选择计算机,另一个选择框将显示值列表华硕笔记本电脑.
我知道如何编码动态选择框,但它不适用于无限的子类别.
这个应该是这样的(根据我的想法).
- User choose a category from the first selectBox
- Send ajax to getcategory.PHP
- getcategory.PHP finds out if there is any child category from the user-chosen first category.
- ajax receive data from getcategory.PHP
- if data exist, jQuery create a new select Box and put in the fetched data into it.
- User choose sub-category from the just added selectBox
- Send ajax to getcategory.PHP
- getcategory.PHP finds out if there is any more child category from the user-chosen sub-category.
- ajax receive data from getcategory.PHP
- if data exist, jQuery create a new select Box and put in the fetched data into it.
- and so on.. until the selectBox have no child category.
我该如何实现这一目标?这是正确的方法吗?或者还有其他解决方法吗?谢谢
解决方法:
<?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>';
}
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。