我正在尝试做的是为产品制作过滤器,我正在从数据库中检索产品,包括图像和一些信息,我想要一些过滤器,如:PRODUCT和BRAND
我有这个代码,但当我选择两个过滤器,如品牌1 +产品2,向我展示品牌1的所有产品和所有产品编号2 ..我希望它结合...
这是我的代码:
HTML + PHP
<div id="row" class="row">
<?php
if(!empty($_GET["category"])){
include('open_conexion.php');
$sql = "SELECT * FROM products WHERE category='".$_GET["category"]."'";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
?>
<div class="col-lg-4" data-category="<?php echo $row['product'];?>" brand="<?php echo $row['brand'];?>" >
<img class="img-circle" src='images/<?php echo $row['imag']; ?>' width="180px;" height="180px;"alt="">
<h4><?php echo $row['product']." ". $row['brand']; ?></h4>
<p><?php echo $row['description'];?> </p>
<p><a class="btn btn-default" href='detalles.php?id=<?php echo $row['id_product']?>'>View Details »</a></p>
</div><!-- /.col-lg-4 -->
<?php } } ?>
</div><!-- /.row -->
Run Code Online (Sandbox Code Playgroud)
和jQuery:
<script>
$('input[type="checkbox"]').click(function() {
if ($('input[type="checkbox"]:checked').length > 0) {
$('.row >div').hide();
$('input[type="checkbox"]:checked').each(function() {
$('.row >div[data-category=' + this.value + ']').show();
$('.row >div[brand=' + this.value + ']' ).show();
});
} else {
$('.row >div').show();
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢
小智 5
我假设您正在尝试使用jquery或javascript过滤产品客户端,而不是使用服务器端进行查询。
保留您的复选框和产品列表,如下所示:
<div><input name="product1" id="product1" data-id="1" class="products" type="checkbox" />Product 1</div>
<div><input name="product2" id="product2" data-id="2" class="products" type="checkbox" />Product 2</div>
<div><input name="product3" id="product3" data-id="3" class="products" type="checkbox" />Product 3</div>
<div><input name="brand1" id="brand1" data-id="1" class="brands" type="checkbox" />Brand 1</div>
<div><input name="brand2" id="brand2" data-id="2" class="brands" type="checkbox" />Brand 2</div>
<div><input name="brand3" id="brand3" data-id="3" class="brands" type="checkbox" />Brand 3</div>
<div class="row">
<div class="product" brand="1" data-category="1">product 1 brand 1</div>
<div class="product" brand="1" data-category="1">product 1 brand 1</div>
<div class="product" brand="2" data-category="1">product 1 brand 2</div>
<div class="product" brand="2" data-category="1">product 1 brand 2</div>
<div class="product" brand="3" data-category="1">product 1 brand 3</div>
<div class="product" brand="3" data-category="1">product 1 brand 3</div>
<div class="product" brand="1" data-category="2">product 2 brand 1</div>
<div class="product" brand="1" data-category="2">product 2 brand 1</div>
<div class="product" brand="2" data-category="2">product 2 brand 2</div>
<div class="product" brand="2" data-category="2">product 2 brand 2</div>
<div class="product" brand="3" data-category="2">product 2 brand 3</div>
<div class="product" brand="3" data-category="2">product 2 brand 3</div>
<div class="product" brand="1" data-category="3">product 3 brand 1</div>
<div class="product" brand="1" data-category="3">product 3 brand 1</div>
<div class="product" brand="2" data-category="3">product 3 brand 2</div>
<div class="product" brand="2" data-category="3">product 3 brand 2</div>
<div class="product" brand="3" data-category="3">product 3 brand 3</div>
<div class="product" brand="3" data-category="3">product 3 brand 3</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Javascript代码应该是这样的,我已经使用jQuery进行了过滤。希望您也尝试使用jQuery。
<script type="text/javascript">
$(function(){
$('.products, .brands').on('click', function(){
var checkedProducts = $('.products:checked');
var checkedBrands = $('.brands:checked');
if(checkedProducts.length || checkedBrands.length){
if(checkedBrands.length === 0){
$('.row > div').hide();
$.each(checkedProducts, function(){
var prdId = $(this).attr('data-id');
$('.row > div[data-category="' + prdId + '"]').show();
});
} else if(checkedProducts.length === 0) {
$('.row > div').hide();
$.each(checkedBrands, function(){
var brandId = $(this).attr('data-id');
$('.row > div[brand="' + brandId + '"]').show();
});
} else {
$('.row > div').hide();
$.each(checkedProducts, function(){
var prdId = $(this).attr('data-id');
$.each(checkedBrands, function(){
var brandId = $(this).attr('data-id');
$('.row > div[data-category="' + prdId + '"][brand="' + brandId + '"]').show();
});
});
}
} else {
$('.row > div').show();
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
而已!您已保存。.如果您有任何问题,请通知我。
| 归档时间: |
|
| 查看次数: |
5976 次 |
| 最近记录: |