【问题标题】:Filter product Price and sort Asc Desc PHP MYSQLI筛选产品价格和排序 Asc Desc PHP MYSQLI
【发布时间】:2017-12-28 13:54:33
【问题描述】:

我希望你永远关心! 我被问到:

  1. 如果我按价格 MIN 到 MAX 过滤产品,可以查看已经OK

  2. 在步骤过滤器(第 1 号)和过滤器 ASC 之后,DESC 条件为什么我的视图页面是“为 foreach() 提供的参数无效”,为什么会这样???

.

<?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> 

【问题讨论】:

  • 您将 and 放在将导致您的错误的顺序之后。 order by 必须是最后一个,group by 倒数第二个。来来去去。
  • 好吧兄弟..你有最好的解决方案。先谢谢了

标签: php mysql sql mysqli filtering


【解决方案1】:

您不能将价格 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);

顺便说一句:您的代码存在 SQL 注入漏洞。这很糟糕,了解更多:http://bobby-tables.com/php

【讨论】:

  • 是的,兄弟...它很棒的解决方案,在此之前先谢谢...但我认为更多的问题。问题是:如果购物车订单(第一个)是好的,但是如果在 pag 之后的动作分页。 2 或 3 或 4 并添加命令,命令没有响应。为什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2014-07-11
  • 1970-01-01
  • 1970-01-01
  • 2015-03-05
  • 2015-04-12
相关资源
最近更新 更多