繁体   English   中英

排序asc desc php,mysql

[英]sort asc desc php, mysql

我有一些用于升序和降序排序的代码,当单击链接时,表将按asc排序,然后它们将按desc排序。 通常,代码可以正常工作,但它们不会从数据库返回第一个字段和最后一个字段。 当desc排序时不返回最后一个,而当asc则不返回第一个字段。 我将在这里介绍一段代码。 有人帮忙吗? 谢谢。

这是请求的链接

echo "<th>ID 
        <a href='sort_user.php?sortItemsId&order=" . (isset($_GET['order'])?!$_GET['order']: 1) . "'>
            <i class='fa fa-sort' aria-hidden='true'></i>
        </a>
        </th>";

if (isset($_REQUEST["sortItemsId"])) {
            $isAsc = isset($_GET['order'])? (bool) !$_GET['order']: 1;
            $sql = "SELECT id, name, number, email, recovery_email, address       FROM users ORDER BY id " .($isAsc?"ASC":"DESC").";";
            $query = mysqli_query($db, $sql);

.....
}

您在代码中嵌套了while循环:

while($row = mysqli_fetch_object($query)) {
    //some code
    //$row is first object
    while($row = mysqli_fetch_object($query)) {
        //printing data
        //$row is replaced by second object and so on
    }
}

我现在将发布带有打印数据的完整代码。 我得到结果,但对desc排序时没有最后一个字段,对asc排序时没有得到第一字段

       if (isset($_REQUEST["sortItemsId"])) {
         $isAsc = isset($_GET['order'])? (bool) !$_GET['order']: 1;
         $sql = "SELECT id, CRMContact, CRMOrganization, username, recovery_email, status FROM tb_users ORDER BY id " .($isAsc?"ASC":"DESC").";";
         $query = mysqli_query($db, $sql);

        if (mysqli_num_rows($query) > 0) {
            while($row = mysqli_fetch_object($query)) {
                echo "<div class='container'><table class='table table-striped'><thead>";
                echo " <tr><th>ID 
                <a href='existing_user.php?sortItemsId&order=" . (isset($_GET['order'])?!$_GET['order']: 1) . "'>
                    <i class='fa fa-sort' aria-hidden='true'></i>
                </a>
                </th> <th>CRM Contact</th><th>CRM Organization</th>";
                echo "<th>Username</th><th>Recovery email</th><th>Status</th><th>Actions</th></tr></thead>";
                $i = 0;
                while ($row = mysqli_fetch_object($query)) {  
                $i++; 
                $id = $row->id;         
                echo "<tbody>
                          <tr>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>$row->id</td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>$row->CRMContact</td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>$row->CRMOrganization</td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'><a href='edit_user.php?idEdit=$row->id'>$row->username</a></td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>$row->recovery_email</td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>$row->status</td>
                            <td style='background-color:" . (($i % 2 == 0) ? '#fff' : '#f9f9f9') . "'>
                                <div class='btn-group'>
                                  <button type='button' class='btn btn-default dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false' style='background-color: #4076BC; color: #fff;'>
                                  <i class='fa fa-fire' aria-hidden='true'></i>

                                    Action <span class='caret'></span>
                                  </button>
                                  <ul class='dropdown-menu'>
                                    <li><a href='view_user.php?viewId=$row->id'>View</a></li>
                                    <li><a href=''>Edit</a></li>
                                    <li><a href='existing_user.php?deleteUser=$row->id'>Delete</a></li>";
                                    ?>
                                    <?php
                                    if ($row->deactivated == 1) {
                                        echo "<li><a href='existing_user.php?deactivateUserAcc=activate&id=$id'>Activate</a></li>";
                                    } else {
                                        echo "<li><a href='existing_user.php?deactivateUserAcc=deactivate&id=$id'>Deactivate</a></li>";
                                    }
                                  "</ul>
                                </div>
                            </td>
                          </tr>
                        </tbody>";
                }
                echo "</table></div>";
            } 
        } else if (mysqli_num_rows($query) == 0) {
            echo "<script type='text/javascript'>alert('Database is empty.');</script>";
        } else {
            echo "<script type='text/javascript'>alert('Something went wrong.');</script>";
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM