产品细化按类别和地区过滤

时间:2018-03-18 08:28:20

标签: javascript php jquery

我目前正在尝试按类别,条件,区域和价格实施产品细化过滤器,我将此网站http://phpcooker.com/product-search-filter-in-php作为参考。但每当我选中一个方框时(例如,我检查一个属于它的2或3个产品的类别),它会在URL中获取catId并且页面只刷新并且不进行过滤。它只是在一侧重新组合相同类别的产品,而在另一侧保留不属于我检查类别的产品。

我有4张桌子 广告表,包含adId,title,catId,regionId,price,condition,description catId,catName的类别表 regionId,regionName的区域表 图像表与imageId,adId,路径,预览

对不起我的英语,但我迫切需要找到解决方案。?

这是我的班级文件

public function getAllCat(){
    $query = "SELECT * FROM category ORDER BY catId";
    $result = $this->db->select($query);
    return $result;

}
    public function getAllRegion(){
    $query = "SELECT * FROM region ORDER BY regionId";
    $result = $this->db->select($query);
    return $result;
}
    public function getAllAds(){
    $query = "SELECT * FROM ads ORDER BY adId DESC";
    $result = $this->db->select($query);
    return $result;
}

public function getPreviewImage($id){
    $query = "SELECT * FROM images WHERE adId = $id AND preview = '1' ";
    $image = $this->db->select($query);
    return $image;

}

这是我的allads.php文件

<?php

//declaration array varible
 $filtercategory = array();
 $filterregion = array();

//finding query string value
if(isset($_REQUEST['filtercategory'])){
  //query string value to array and removing empty index of array
$filtercategory = array_filter(explode("-",$_REQUEST['filtercategory']));
 }
if(isset($_REQUEST['filterregion'])){

$filterregion = array_filter(explode("-",$_REQUEST['filterregion']));
}
?>
<main class="cd-main-content">
    <div class="Ads-Container">

            <?php 
                    $query = "SELECT * FROM ads ORDER BY adId";
                    //filter query start
                      if(!empty($filtercategory)){
                       $categorydata =implode("','",$filtercategory);
                       $query .= " and catId in('$categorydata')";
                      }

                      if(!empty($filterregion)){
                       $regiondata =implode("','",$filterregion);
                       $query .= " and regionId in('$regiondata')";
                      }

                 //filter query end
                    $post = $db->select($query);
                    if($post){
                        while($result = $post->fetch_assoc()){
            ?>

        <div class="ads-column_2">
            <div class="ads-column-thumbnail">
                <?php 
                             $preview = $ad->getPreviewImage($result["adId"]);
                             if($preview){
                                while($rresult = $preview->fetch_assoc()){
                        ?>
                <img src="/afro-circle/<?php echo $rresult['path']?>" alt="" class="image-responsive">
                 <?php } } ?>
                <div class="ads-preview-details">
                   <center>
                      <h4><a href="addetails.php?adid=<?php echo $result['adId']; ?>"><?php echo $result['title']; ?></a></h4>
                      <h4 class="">FCFA <?php echo number_format($result['price']); ?></h4>
                   </center>
                </div>
                <div class="space-ten"></div>
                <div class="btn-ground text-center">
                   <a href="addetails.php?adid=<?php echo $result['adId']; ?>"><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#product_view">Quick View</button></a>
                </div>
                <div class="space-ten"></div>
            </div>
        </div>

        <?php } } ?>

    </div>          
    <div class="cd-filter filter-is-visible">
        <form>

            <div class="cd-filter-block">
                <h4>Categories <span class="spanbrandcls" style="float:right; visibility:hidden;"><a href="javascript:void(0);"><img src="refine/images/reset.png" alt="reset" title="reset"></a></span></h4>

                    <ul class="cd-filter-content cd-filters list">

                    <?php
                                  $getCategory = $category->getAllCat();
                                  if($getCategory){
                                    while($result = $getCategory->fetch_assoc()){
                    ?>
                    <li>
                        <input type="checkbox"  class="filter filtercategory" value="<?php echo $result['catId']; ?>" <?php if(in_array($result['catName'],$filtercategory)){ echo"checked"; } ?> >
                        <label class="checkbox-label" id="Category" ><?php echo $result['catName']; ?></label>
                    </li>

                    <?php } } ?>


                </ul> <!-- cd-filter-content -->                    
            </div> <!-- cd-filter-block -->

            <div class="cd-filter-block">
                <h4>Size <span class="spansizecls" style="float:right; visibility:hidden;"><a href="javascript:;"><img src="refine/images/reset.png" alt="reset" title="reset"></a></span></h4>

                <div class="cd-filter-content">
                    <div class="cd-select cd-filters">      
                        <select class="filter scheck" name="subcatId">
                            <option data-type="sizes" value="">Item Condition</option>

                            <option data-type="sizes" value="1">New</option>

                            <option data-type="sizes" value="2">Used</option>

                        </select>

                    </div> <!-- cd-select -->
                </div> <!-- cd-filter-content -->
            </div> <!-- cd-filter-block -->


            <div class="cd-filter-block">
                <h4>Region <span class="spancolorcls" style="float:right; visibility:hidden;"><a href="javascript:;"><img src="refine/images/reset.png" alt="reset" title="reset"></a></span></h4>

                <ul class="cd-filter-content cd-filters list">
                <?php
                                  $getRegion = $region->getAllRegion();
                                  if($getRegion){
                                    while($result = $getRegion->fetch_assoc()){
                 ?>

                    <li>
                        <input type="radio" class="filter filterregion" value="<?php echo $result['regionId']; ?>" <?php if(in_array($result['regionName'],$filterregion)){ echo"checked"; } ?>>
                        <label class="radio-label" for="radio1"><?php echo $result['regionName']; ?></label>
                    </li>

                <?php } } ?>        
                </ul> <!-- cd-filter-content -->
            </div> <!-- cd-filter-block -->
        </form>

        <a href="#0" class="cd-close"><i class="fa fa-close"></i> Close</a>
    </div>



    <a href="#0" class="cd-filter-trigger"><i class="fa fa-filter"></i> Search by:</a>
    <div class="clear"></div>

这是我的JS

<script>
 $(function(){
  $('.filter').click(function(){
   var filtercategory = multiple_values('filtercategory');
   var filterregion = multiple_values('filterregion');

   var url ="allads.php?filtercategory="+filtercategory+"&filterregion="+filterregion;
   window.location=url;
  });

 });


 function multiple_values(inputclass){
  var val = new Array();
  $("."+inputclass+":checked").each(function() {
   val.push($(this).val());
  });
 return val.join('-');
}

0 个答案:

没有答案