结果过滤器不起作用

时间:2015-12-19 19:32:29

标签: php mysqli

我已经使用php创建了一个过滤结果,但是当我过滤结果它没有向我显示任何产品时,它不适合我。产品尺寸和产品颜色列在不同的表格中。当我过滤并点击提交时它没有向我显示任何产品没有产品可供我使用虽然任何人可以帮助我,请在下面是我使用的代码请告诉我。

<form method="POST" action="product.php?cat=<?php echo $cat; ?>">
    <div class="price-box">
        <h5>Price</h5>
        <input type="hidden" name='price_range' class="range-slider" min="0" max="600" value="23" />
    </div>
    <div id="filters">
    <div class="size-box">
    <h5>size</h5>
    <ul>
        <li> <input type="checkbox" id="active" name="size[]" value="S"> <span class="check-txt">S (10)</span></li>
        <li> <input type="checkbox" id="In Stock" name="size[]" value="XL"> <span class="check-txt">XL (10)</span></li>
        <li> <input type="checkbox" id="large" name="size[]" value="L"> <span class="check-txt">L (10)</span></li>
    </ul>
    </div>
    </div>

    <div class="color-box">
        <h5>color</h5>
        <ul>
            <li> <input type="checkbox" name="color[]" value="White"> <span class="check-txt">White (2)</span></li>
            <li> <input type="checkbox" name="color[]" value="Black"> <span class="check-txt">Black (3)</span></li>
            <li> <input type="checkbox" name="color[]" value="Orange"> <span class="check-txt">Orange (3)</span></li>
            <li> <input type="checkbox" name="color[]" value="Blue"> <span class="check-txt">Blue (4)</span></li>
            <li> <input type="checkbox" name="color[]" value="Green"> <span class="check-txt">Green (2)</span></li>
            <li> <input type="checkbox" name="color[]" value="Yellow"> <span class="check-txt">Yellow (1)</span></li>
        </ul>
        </div>
        <input type="submit" name="filter" value="Filter Results" />
    </form>

这是我的PHP代码

    <?php
    $num_rec_per_page=9;
    if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; };
        $start_from = ($page-1) * $num_rec_per_page;                    
        $query = "SELECT * FROM products WHERE product_category = '$cat'";
            if(isset($_POST['filter'])) {
                $query = "SELECT p.* FROM products as p
                JOIN prod_size as ps ON (p.prod_id=ps.prod_id)
                JOIN prod_color as pc ON (p.prod_id=pc.prod_id)
                WHERE ";
                $range = $_POST['price_range'];
                $rg = explode(',', $range);
                $range1 = $rg[0];
                $range2 = $rg[1];
                if($range1!='' && $range2!='') {    
                     $query .= " p.product_price BETWEEN (".$range1." AND ".$range2.") AND ";   
                }

                if(isset($_POST['size']) && is_array($_POST['size'])) {
                     $size = implode(',',$_POST['size']);
                     $query .= " ps.size IN ('".$size."') AND ";        
                }

                if(isset($_POST['color']) && is_array($_POST['color'])) {
                     $color = implode(',',$_POST['color']);
                     $query .= " pc.color  IN ('".$color."') AND ";     
                }
                     $query .= " p.prod_id > 0 ";
            }   

            $query .= " LIMIT ".$start_from.",".$num_rec_per_page;
            $get_prod = mysqli_query($connection, $query);

            $total = mysqli_num_rows($get_prod);
            while($prod = mysqli_fetch_assoc($get_prod)) {
       ?>
        <li class='resultblock' data-tag='<?php echo $prod['status']; ?>'>
            <span class="cbp-vm-image">
            <img src="admin/uplaods/<?php echo $prod['product_image']; ?>" alt="">
                 <ul>
                    <li><a href=""><i class="fa fa-heart"></i></a></li>
                    <li><a href=""><i class="fa fa-refresh"></i></a></li>
                    <li><a href=""><i class="fa fa-search"></i></a></li>
                 </ul>
            </span>

            <p class="cbp-vm-title"><?php echo $prod['product_name']; ?></p>
                 <ul class="cbp-vm-price">
                    <li>$<?php echo $prod['product_price']; ?></li>
                    <li class="pro-rating"><span><i class="fa fa-star"></i></span> <span><i class="fa fa-star"></i></span> <span><i class="fa fa-star"></i></span> <span><i class="fa fa-star"></i></span> <span><i class="fa fa-star"></i></span></li>
                 </ul>
            <a class="cbp-vm-icon cbp-vm-add" href="details.php?pid=<?php echo $prod['prod_id'];?>">View Details</a>
            </li>
            <?php } ?>
        </ul>

谢谢

1 个答案:

答案 0 :(得分:0)

我认为你的问题在这里:

在使用下面的代码之前你必须有$ cat = $ _GET ['cat']:

 $cat = $_GET['cat']; //add this before
 $query = "SELECT * FROM products WHERE product_category = '$cat'";