我从mysqli数据中获取产品,以及如何在输入类型的帮助下对结果中的产品进行排序

时间:2017-08-22 03:59:28

标签: php mysqli

<div class="row item-display">
              <?php
               if(isset($_GET['offers'])){
                    $cpro_query ="SELECT * FROM product WHERE offers!='no offers'";
                  }elseif(isset($_POST['search-title'])){
                    $search =$_POST['search-title'];
                    $cpro_query ="SELECT * FROM product WHERE tag LIKE '%$search%'";
                  }
                  else{
                  $cpro_query = "SELECT * FROM product";
              }
              if(isset($cat_name)){
                $cpro_query.=" WHERE cat_name='$cat_name'";
              }elseif(isset($brand_name)){
                $cpro_query.=" WHERE brand_name='$brand_name'";
              }else{
              $cpro_query.=" ORDER BY product_id DESC";
            }

              $cpro_run = mysqli_query($con,$cpro_query);
              if(mysqli_num_rows($cpro_run)>0){
                while ($cpro_row = mysqli_fetch_array($cpro_run)) {
                  $pro_id = $cpro_row['product_id'];
                  $pro_title = $cpro_row['title'];
                  $pro_image = $cpro_row['image1'];
                  $pro_desc = substr($cpro_row['description'],0,100);
                  $pro_cost = $cpro_row['cost'];
                  $pro_rating = $cpro_row['rating'];
              ?>
              <div class="col-md-3">
                <a href='single-item-display.php?pro_id=<?php echo $pro_id; ?>'><div class="panel">
                  <img src="images/<?php echo $pro_image;?>" class="img-responsive center-block" alt="item" >
                  <p><?php echo $pro_desc;?></p>
                  <p>Price:<span class="fa fa-rupee"><?php echo $pro_cost;?>/-</span></p>
                  <p><?php require('inc/rating.php');?></p>
                  <h4 style="color:#E80C4F;"><?php echo $pro_title;?></h4>
                </div></a>
              </div>
              <?php
              }
            }else{
              echo "<center><span style='color:red;'>No products yet<span></center>";
            }
              ?>

            </div>
<ul class="dropdown-menu">
                   <li class="dropdown">
                      <a href="product-categorywise.php?lh=<?php echo $lh;?>">Price Low to High </a>
                      <a href="product-categorywise.php?hl=<?php echo $hl?>">Price High to Low </a>
                      <a href="product-categorywise.php?fa=<?php echo $lh?>">Fresh Arrivals </a>
                      <a href="product-categorywise.php?di=<?php echo $lh?>">Discount </a>
                    </li>
                  </ul>

我如何在产生的产品中从高价格到低价格或从低到高的价格对产品进行分类。不是来自数据库中的所有产品。我有按照颜色,品牌,类型,尺寸等排序列表。如何编写查询或者如何制作功能。

2 个答案:

答案 0 :(得分:0)

只需添加查询,例如
$ cpro_query。=“ORDER BY product_id DESC,price DESC”; //按产品ID和价格排序为desc order

答案 1 :(得分:0)

$orderfield = 'product_id';
if (isset($_GET['orderfield'])) {
    if ($_GET['orderfield']=='someotherfield') {
        $orderfield = 'someotherfield';
    }
}

$orderdir = 'DESC';
if (isset($_GET['orderdir'])) {
    if ($_GET['orderdir']=='asc') {
        $orderdir = 'ASC';
    }
}

$cpro_query.=" ORDER BY ' . $orderfield . ' ' . $orderdir;