php
  • mysql
  • sql
  • 2017-05-26 61 views 0 likes 
    0

    我有一些升序和降序排序的代码,当点击链接时,表格会按顺序排序,接着它们将排序为desc。通常代码正在工作,但它们不会从数据库返回第一个和最后一个字段。当sort desc不会最后返回时,当asc不返回第一个字段时。我会在这里找到一段代码。有人帮忙?谢谢。sort asc desc php,mysql

    这是链接请求

    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); 
    
    ..... 
    } 
    
    +0

    SQL看起来很好。我认为你的问题是在打印出数据的代码中。 – Jens

    +0

    您确定没有从数据库获取行吗?在某个地方显示结果的时候,你的循环可能会出错吗? – ElChupacabra

    回答

    0

    我将张贴现在已满代码打印数据。我得到的结果,但最后一个字段排序倒序时不要和排序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>"; 
         } 
        } 
    
    +0

    您已嵌套while($ row = mysqli_fetch_object($ query))。嵌套循环覆盖现有的$行。 – ElChupacabra

    +0

    你应该删除第一个或第二个while($ row = mysqli_fetch_object($ query))循环,就像@ElChupacabra所说,正在覆盖现有的行 – nacho

    +0

    就是这样,现在工作很好,谢谢你的帮助 –

    1

    你嵌套了,而在你的代码循环时,没有得到第一场:

    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 
        } 
    } 
    
    相关问题