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

How can I make data sortable by date in an SQL populated table?

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

我的代码如下:

<?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();
?>

我的问题是使用我已有的数据,有什么方法可以按日期制作 table 排序 table 吗?

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