如何在SQL填充表中按日期对数据进行排序?

时间:2015-06-25 09:42:51

标签: php jquery mysql sql html-table

目前我有一个页面在一个页面上显示系统上的所有订单,这部分工作正常,不是问题。当我们达到一定数量的订单时,问题就出现了,我想让它易于消化,因此一次显示在页面上的最大订单数为15,数据可以排序,以便加快搜索速度。

我的代码如下:

<?php
    $servername = "localhost";
    $username = "qwe";
    $password = "qwe";
    $dbname = "qwe";

    // Create connection
        $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    $sql = "SELECT * FROM Orders ORDER BY ID DESC";                         
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        echo "<table class='table table-hover' id='orders'><tr style='font-size:18px;'><th style='text-align:center;'>ID</th><th style='text-align:center;'>Customer</th><th style='text-align:center;'>Material</th><th style='text-align:center;'>Quantity</th><th style='text-align:center;'>Delivery</th><th style='text-align:center;'>Post Code</th><th style='text-align:center;'>Cost / Tonne</th><th style='text-align:center;'>Total</th><th style='text-align:center;'>Paid</th><th style='text-align:center;'>Staff</th><th style='text-align:center;'>Timestamp</th></tr>";
        // output data of each row
        while($row = $result->fetch_assoc()) { 
            echo "<tr style='font-size:16px; text-align:center;'><td><span style='color:#0b78e5;'>SGL</span><a href='#' style='color:#0b78e5;'>".$row["ID"]."</a></td><td>".$row["Customername"]."</td><td>".$row["Material"]."</td><td>".$row["Quantity"]."</td><td><input type='checkbox' disabled". ($row["Delivery"] == 'Yes' ? " checked" : "")  ."></td><td>".$row["Postcode"]."</td><td>&pound;".$row["CostPerTonne"]."</td><td>&pound;".$row["TotalCost"]."</td><td>".$row["Paid"]."</td><td>".$row["Username"]."</td><td>".$row["Logged"]."</td></tr>";
        }
        echo "</table>";
    } else {
        echo "There are 0 orders in the system";
    }
    $conn->close();
?>

我的问题是使用我已有的数据是否有任何方法可以按日期对表格进行排序?

1 个答案:

答案 0 :(得分:0)

要按日期排序,您必须创建一个具有时间戳数据类型的列并将默认值保持为current_timestamp,或者猜测ID较大的订单是最新的,您当前的代码将按日期排序。您可以使用数据表https://www.datatables.net/,数据表按所有列的相反顺序提供排序,并搜索使您更轻松地搜索项目