【问题标题】:How can I make data sortable by date in an SQL populated table?如何在 SQL 填充表中按日期对数据进行排序?
【发布时间】:2015-09-11 20:16:48
【问题描述】:

目前我有一个页面在一个页面上显示系统上的所有订单,这部分工作正常,不是问题。当我们达到一定数量的订单时会出现问题,我想让它易于消化,因此一次在页面上显示的最大订单数为 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();
?>

我的问题是使用我已有的数据,有什么方法可以让表格按日期排序?

【问题讨论】:

  • 看起来你需要分页。在堆栈溢出中搜索相同的内容

标签: php jquery mysql sql html-table


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2013-12-18
    • 1970-01-01
    • 2011-09-08
    • 2019-01-17
    • 2020-06-04
    • 2015-07-08
    相关资源
    最近更新 更多