筛选产品价格和排序Asc Desc PHP MYSQLI

时间:2017-07-22 14:51:45

标签: php mysql sql mysqli filtering

我希望你一直关心! 我被问到:

  1. 如果我将产品按表格价格MIN过滤到MAX,则可以查看已经确定

  2. 在步骤过滤器(No.1)和过滤器ASC之后,DESC条件为什么我的视图页面是"为foreach()"提供的参数无效; ,为什么呢???

  3. <?php 
        include("config.php");
        $all_brand=$db->query("SELECT distinct brand FROM `products` WHERE category_id = '1' GROUP BY brand");
        // Filter query
        $sql= "SELECT distinct * FROM `products` WHERE category_id = '1'";
    
        if(isset($_GET['brand']) && $_GET['brand']!="") :
            $sql.=" AND brand IN ('".implode("','",$_GET['brand'])."')";
        endif;
    
        if(isset($_GET['sort_price']) && $_GET['sort_price']!="") :
            if($_GET['sort_price']=='price-asc-rank') :
                $sql.=" ORDER BY price ASC";
            elseif($_GET['sort_price']=='price-desc-rank') :
                $sql.=" ORDER BY price DESC";
            endif;
        endif;
    
        // filter by input price
        if(isset($_GET['min']) && $_GET['min']!="") :
         $sql.="AND price >= '".$_GET['min']."' ";
        endif;
    
        if(isset($_GET['max']) && $_GET['max']!="") :
         $sql.="AND price <= '".$_GET['max']."' ";
        endif;
    
        $all_product=$db->query($sql);
        ?>
    

    和我的表单:

        *******filter ASC and DESC **********
    <div class="panel list">                  
                          <div class="col-sm-2">
                                <select name="sort_price" class="sort_rang" id="sort">
                                     <option value="">Paling baru</option>
                          <option <?=(isset($_GET['sort_price'])&&($_GET['sort_price']=='price-asc-rank')? 'selected="selected"' : '' )?>  value="price-asc-rank">Harga:Rendah ke tinggi </option>
    
                          <option <?=(isset($_GET['sort_price'])&&($_GET['sort_price']=='price-desc-rank') ? 'selected="selected"' : '' )?>  value="price-desc-rank">Harga:Tinggi ke rendah</option>
                                </select>
                          </div>
                        </div>
    
        <!-- filter price -->
                            <div class="sidebar-row">
                            <h4>RENTANG HARGA</h4>
                                  <input type="text" name="min" id="min" placeholder=" Mulai dari harga" onkeypress="return AllowOnlyNumbers(event);" value="<?php echo isset($_GET['min']) ? $_GET['min'] : ''; ?>"> <br>
                                    <br>
                                  <input type="text" name="max" id="max" placeholder=" Sampai dgn harga" onkeypress="return AllowOnlyNumbers(event);" value="<?php echo isset($_GET['max']) ? $_GET['max'] : ''; ?>"> <br>
                                    <br>
                                  <input type="submit" class="sort_rang" value="Tampilkan">
                            </div> 
    

1 个答案:

答案 0 :(得分:0)

您无法将价格WHERE置于ORDER之后:

include("config.php");
$all_brand=$db->query("SELECT distinct brand FROM `products` WHERE category_id = '1' GROUP BY brand");
// Filter query
$sql= "SELECT distinct * FROM `products` WHERE category_id = '1'";

if(isset($_GET['brand']) && $_GET['brand']!="")
    $sql.=" AND brand IN ('".implode("','",$_GET['brand'])."')";

// filter by input price
if(isset($_GET['min']) && $_GET['min']!="")
    $sql.="AND price >= '".$_GET['min']."' ";

if(isset($_GET['max']) && $_GET['max']!="")
    $sql.="AND price <= '".$_GET['max']."' ";

if(isset($_GET['sort_price']) && $_GET['sort_price']!="") :
    if($_GET['sort_price']=='price-asc-rank')
        $sql.=" ORDER BY price ASC";
    elseif($_GET['sort_price']=='price-desc-rank')
        $sql.=" ORDER BY price DESC";
endif;


$all_product=$db->query($sql);

BTW:您的代码易受SQL注入攻击。这很糟糕,了解详情:http://bobby-tables.com/php