【问题标题】:How to get_results from the database and display sorted in a table如何从数据库中获取结果并在表格中排序显示
【发布时间】:2013-10-25 14:12:45
【问题描述】:

我正在尝试显示我在 Wordpress 插件中创建的表格中的信息(下面的代码),我可以毫无问题地显示在表格中,但我无法弄清楚如何以某种排序顺序显示,即 I.E.按行 ID 排序。

<?php

    echo "<table class='widefat'>"; // start a table in HTML
    echo "<thead>
            <tr>
                <th>Select</th>
                <th>Id</th>
                <th>Serial Number</th>
                <th>UserID</th>
                <th>Owner</th>
                <th>Name</th>
                <th>AreaCode</th>
                <th>Number</th>
                <th></th>
            </tr>
          </thead>
          <tfoot>
            <tr>
                <th>Select</th>
                <th>Id</th>
                <th>Serial Number</th>
                <th>UserID</th>
                <th>Owner</th>
                <th>Name</th>
                <th>Area Code</th>
                <th>Number</th>
                <th></th>
            </tr>
          </tfoot>
          <tbody>";


    $result = $wpdb ->get_results( "SELECT * FROM wp_new_table ");
    foreach($result as $row){
        echo "<tr><td><input name='ID_selected' type='radio' value='" . $row->id. "' /></td><td>" . $row->id. "</td><td>" . $row->sn. "</td><td>" . $row->userid. " </td><td>" . get_user_display_name($row->userid) ."</td><td>"  . $row->name. "</td><td>" . $row->areacode. "</td><td>" . $row->phone. "</td><td>

        <a href='" . $_SERVER['REQUEST_URI'] . "&dbviewedit=" . $row->id . "'>View/Edit</a></td></tr>";


    }

    echo "</tbody></table>";
    ?>

【问题讨论】:

  • 我想通了 :-) $result = $wpdb ->get_results("SELECT * FROM wp_new_table");变为 $result = $wpdb ->get_results("SELECT * FROM wp_new_table ORDER BY id DESC ");然后我刚刚发现约翰史密斯的答案!谢谢老兄!

标签: php mysql wordpress sorting html-table


【解决方案1】:

您必须调整 SQL 请求,因为结果已经未排序

SELECT columnname FROM tablename ORDER BY columnname ASC|DESC

我希望这就是你要找的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-02
    • 2013-08-24
    • 2014-07-17
    • 1970-01-01
    • 2018-04-11
    • 2015-11-26
    • 2020-01-02
    • 2020-04-21
    相关资源
    最近更新 更多