排序asc desc php,mysql

时间:2017-05-26 11:07:24

标签: php mysql sql

我有一些升序和降序的代码,当点击链接时,表格将被排序为asc,然后它们将被排序为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);

.....
}

2 个答案:

答案 0 :(得分:1)

您在代码中嵌套了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
    }
}

答案 1 :(得分:0)

我现在将发布打印数据的完整代码。我得到了结果但是在排序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>";
        }
    }