如何使用PHP对从MySQL数据库中提取的记录进行排序?

时间:2013-07-10 13:14:38

标签: php

我正在为我的网站编写高级搜索代码。我想根据4个参数 - 客户名称,公司名称,订单数量和总订单价值对从MySQL数据库中提取的记录进行排序。搜索工作非常好,并且还根据客户名称和公司名称对值进行排序。但问题是我无法根据订单数量和总订单价值对行进行排序。

首先看看我的任务界面 -

enter image description here

我使用4个表来完成这个搜索引擎的排序。以下是表格:

  1. address_book table:
  2. enter image description here

    1. 客户表:
    2. enter image description here

      1. 订单表:
      2. enter image description here

        1. orders_products表:
        2. enter image description here

          这是我的PHP代码,用于搜索和排序:

          <?php
          require_once 'includes/config.php';
          ?>
          <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
          <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
          <link rel="stylesheet" type="text/css"
                href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/ui-lightness/jquery-ui.css">
          <script type="text/javascript">
              $(document).ready(function () {
                  //$("#datepicker").datepicker();
                  $("#txtstartdate").datepicker({
                      dateFormat: "yy-mm-dd",
                  });
                  $("#txtenddate").datepicker({
                      dateFormat: "yy-mm-dd",
                  });
              });
          </script>
          <div>
              <form name="frm1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
                  <table width="100%" border="0" align="center">
                      <tr>
                          <th>Start Date:</th>
                          <td><input type="text" name="txtstartdate" id="txtstartdate"
                                     value="<?php if (isset($_POST['btnsearch'])) {
                                         echo $_POST['txtstartdate'];
                                     }
                                     else {
                                         echo '';
                                     } ?>"/></td>
                      </tr>
                      <tr>
                          <th>End Date:</th>
                          <td><input type="text" name="txtenddate" id="txtenddate"
                                     value="<?php if (isset($_POST['btnsearch'])) {
                                         echo $_POST['txtenddate'];
                                     }
                                     else {
                                         echo '';
                                     } ?>"/></td>
                      </tr>
                      <tr>
                          <th>Customer Name:</th>
                          <td><input type="text" name="txtcustomername" id="txtcustomername"
                                     value="<?php if (isset($_POST['btnsearch'])) {
                                         echo $_POST['txtcustomername'];
                                     }
                                     else {
                                         echo '';
                                     } ?>"/></td>
                      </tr>
                      <tr>
                          <th>Company Name:</th>
                          <td><input type="text" name="txtcompanyname" id="txtcompanyname"
                                     value="<?php if (isset($_POST['btnsearch'])) {
                                         echo $_POST['txtcompanyname'];
                                     }
                                     else {
                                         echo '';
                                     } ?>"/></td>
                      </tr>
                      <tr>
                          <th>Sorting:</th>
                          <td><select name="selsorting" id="selsorting">
                                  <option
                                      value='customer_name' <?php if (isset($_POST['btnsearch']) && $_POST['selsorting'] == 'customer_name') {
                                      echo "selected='selected'";
                                  } ?>>
                                      Customer Name
                                  </option>
                                  <option
                                      value="company_name" <?php if (isset($_POST['btnsearch']) && $_POST['selsorting'] == 'company_name') {
                                      echo "selected='selected'";
                                  } ?>>
                                      Company Name
                                  </option>
                                  <option
                                      value="order_quantity" <?php if (isset($_POST['btnsearch']) && $_POST['selsorting'] == 'order_quantity') {
                                      echo "selected='selected'";
                                  } ?>>
                                      Order Quantity
                                  </option>
                                  <option
                                      value="total_order_value" <?php if (isset($_POST['btnsearch']) && $_POST['selsorting'] == 'total_order_value') {
                                      echo "selected='selected'";
                                  } ?>>
                                      Total Order Value
                                  </option>
                              </select></td>
                      </tr>
                      <tr>
                          <td>&nbsp;</td>
                          <td><input type="submit" name="btnsearch" value="Search"/>
                      </tr>
                  </table>
              </form>
          </div>
          <?php
          if (isset($_POST['btnsearch'])) {
              $where = array();
          
              if (isset($_POST['txtstartdate']) && !empty($_POST['txtstartdate'])) {
                  $startdate = $_POST['txtstartdate'];
                  $where[]   = " orders.date_purchased >= '$startdate' ";
              }
              if (isset($_POST['txtenddate']) && !empty($_POST['txtenddate'])) {
                  $enddate = $_POST['txtenddate'];
                  $where[] = " orders.date_purchased <= '$enddate' ";
              }
              if (isset($_POST['txtcustomername']) && !empty($_POST['txtcustomername'])) {
                  $customername = $_POST['txtcustomername'];
                  $where[]      = " customers.customers_firstname LIKE '%$customername%' or customers.customers_lastname LIKE '%$customername%' ";
              }
              if (isset($_POST['txtcompanyname']) && !empty($_POST['txtcompanyname'])) {
                  $companyname = $_POST['txtcompanyname'];
                  $where[]     = " address_book.entry_company LIKE '%$companyname%' ";
              }
              if (isset($_POST['selsorting'])) {
                  $sorting = $_POST['selsorting'];
                  $where[] = " 1 = 1 GROUP BY customers.customers_id ORDER BY $sorting ";
              }
          
              if (count($where) > 0) {
                  $query['where'] = " WHERE " . implode(" AND ", $where);
              }
          
              $sql = "SELECT customers.customers_id, CONCAT(customers.customers_firstname, ' ', customers.customers_lastname) AS `customer_name`, address_book.entry_company AS `company_name`, orders_products.final_price AS total_order_value
              FROM customers
              LEFT JOIN orders ON orders.customers_id = customers.customers_id
              LEFT JOIN address_book ON address_book.customers_id = customers.customers_id
              LEFT JOIN orders_products ON orders_products.orders_id = orders.orders_id " . $query['where'];
          
              $rs = mysql_query($sql);
              echo "<table width='100%' align='center' border='1' cellpadding='5' cellspacing='0'>";
              echo "<tr><th>Customer ID</th><th>Customer Name</th><th>Company Name</th><th>Order Quantity</th><th>Order Total</th></tr>";
              while ($row = mysql_fetch_array($rs)) {
                  $sql2 = "SELECT COUNT(*) AS 'quantity' FROM `orders` WHERE `customers_id` = " . $row['customers_id'];
                  $rs2  = mysql_query($sql2);
                  while ($row2 = mysql_fetch_array($rs2)) {
                      $sql3 = "SELECT SUM(`final_price`) AS 'total_order_value' FROM `orders_products` WHERE `orders_id` IN (SELECT `orders_id` FROM `orders` WHERE `customers_id` = " . $row['customers_id'] . ")";
                      $rs3  = mysql_query($sql3);
                      while ($row3 = mysql_fetch_array($rs3)) {
                          echo "<tr><td>&nbsp;" . $row['customers_id'] . "</td>";
                          echo "<td>&nbsp;" . $row['customer_name'] . "</td>";
                          echo "<td>&nbsp;" . $row['company_name'] . "</td>";
                          echo "<td>&nbsp;" . $row2['quantity'] . "</td>";
                          echo "<td>&nbsp;" . $row3['total_order_value'] . "</td></tr>";
                      }
                  }
              }
              echo "</table>";
          }
          ?>
          

          我不了解如何对订单数量和总订单价值进行排序,因为它们不在主左连接查询中,并且它们是即时生成的。如果我使用聚合函数COUNT和SUM来计算主左连接查询中的订单数量和总订单价值,那么结果就不准确。但是,如果我在内部查询中计算这些,那么我不明白如何根据这两个参数进行排序。

          安迪的帮助将不胜感激。

          提前致谢。

1 个答案:

答案 0 :(得分:0)

您应该使用array_multisort()数组函数对多维数组进行排序。