2016-10-21 49 views
0

你能帮助我如何从MySQL表中排序数据吗?使用php对mysql表中的数据进行排序

我想排序是通过表头:

<?php 
    if(isset($_POST['search'])) { 
     $valueTosearch = $_POST['searchvalue']; 
     $query = "SELECT * FROM `admin` WHERE CONCAT(`id`, `name`, `gender`, `user_group`, `date_registered`) LIKE '%".$valueTosearch."%'"; 
     $search_result = filterTable($query); 
    } else { 
     $query = "SELECT * FROM `admin`"; 
     $search_result = filterTable($query); 
    } 

    function filterTable($query) 
    { 
     include('config/dbconnect.php'); 
     $filter_result = mysqli_query($con, $query); 
     return $filter_result; 
    } 
?> 

<form method='post' action=''> 
    <div><input type = 'text' name = 'searchvalue' placeholder="search by name"> 
     <span> 
      <div style='margin-bottom:3px; margin-top:3px'> 
       <input id='gradient' class='search-btn' type = 'hidden' name = 'search' value = 'search'> 
      </div> 
     </span> 
     <div style="height: auto"> 
      <table id='responsive_table'> 
       <thead> 
        <tr> 
         <th scope="col"><a href="sort">name</a></th> 
         <th scope="col"><a href="sort">sex</a></th> 
         <th scope="col"><a href="sort">user group</a></th> 
         <th scope="col"><a href="sort">date register</a></th> 
        </tr> 
       </thead> 
       <?php while($row = mysqli_fetch_array($search_result)): ?> 
       <tbody> 
        <tr> 
         <td scope="row" data-label='name'><?php echo $row['name']; ?></td> 
         <td data-label='sex'><?php echo $row['gender']; ?></td> 
         <td data-label='user group'><?php echo $row['user_group']; ?></td> 
         <td data-label='date register'><?php echo $row['date_registered']; ?></td> 
        </tr> 
       </tbody> 
       <?php endwhile; ?> 
      </table> 
    </div> 
</form> 
+0

你的意思是,你正在寻找的东西是这样的:https://datatables.net/examples/styling/bootstrap .html? – Twinfriends

+0

你可以使用JQuery吗? – Alessandro

+0

究竟@Twinfriends – Ryan

回答

2

您可以修改您的查询,如下排序:

sql > select * from <table name> order by <column name>; 

默认排序为降序你可以像

sql > select * from <table name> order by <column name> desc; 
+0

它不是他正在寻找的东西。 – Twinfriends

1

如果你可以使用jQuery其他升序,它非常简单,您只需添加以下javascript代码:

$(document).ready(function() { 
    $("#responsive_table").DataTable({ 
     ordering: true, 
     searching: true 
    }); 
}); 

一个完整的示例,请参见下面的代码:

<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
     <meta charset="utf-8"> 
 
     <meta name="viewport" content="width=device-width"> 
 
     <title>JS Bin</title> 
 
     <script src="https://code.jquery.com/jquery-3.1.0.js"></script> 
 
     <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script> 
 
     <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css"> 
 
     <script> 
 
     $(document).ready(function() { 
 
      $("#responsive_table").DataTable({ 
 
       ordering: true, 
 
       searching: true 
 
      }); 
 
     }); 
 
     </script> 
 
    </head> 
 
    <body> 
 
    <form method='post' action=''> 
 
       <div style="height: auto"> 
 
       <table id='responsive_table'> 
 
        <thead> 
 
         <tr> 
 
          <th scope="col">name</th> 
 
          <th scope="col">sex</th> 
 
          <th scope="col">user group</th> 
 
          <th scope="col">date register</th> 
 
         </tr> 
 
        </thead> 
 
        <tbody> 
 
         <tr> 
 
          <td scope="row" data-label='name'>HERE</td> 
 
          <td data-label='sex'>Your</td> 
 
          <td data-label='user group'>data</td> 
 
          <td data-label='date register'>loaded</td> 
 
         </tr> 
 
         <tr> 
 
          <td scope="row" data-label='name'>via</td> 
 
          <td data-label='sex'>PHP</td> 
 
          <td data-label='user group'>loop</td> 
 
          <td data-label='date register'>bye</td> 
 
         </tr> 
 
        </tbody> 
 
       </table> 
 
       </div> 
 
      </form> 
 
    </body> 
 
    </html>

+0

谢谢@allesandro – Ryan

+0

Yay。唯一的答案是正确的,真的有助于...并没有upvotes为你。但是另外两个人收到了没有人关心的答案。今天怎么了?至少,亚历山德罗的回答很好。你是唯一理解这个问题的人。认为其他人只是读了标题。 – Twinfriends

+1

谢谢@Twinfriends – Alessandro

相关问题