使用php从mysql表中排序数据

时间:2016-10-21 07:07:16

标签: php html css

你能帮我解决一下如何从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>

3 个答案:

答案 0 :(得分:2)

您可以按如下方式修改查询以进行排序:

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

默认排序是升序,其他用于降序,你可以这样做

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

答案 1 :(得分:1)

为什么不使用 order by 子句:

SELECT * FROM table ORDER BY列;

Order By Reference

答案 2 :(得分:1)

如果您可以使用JQuery,它非常简单,您只需要添加以下JavaScript代码:

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

有关完整示例,请参阅以下代码:

&#13;
&#13;
<!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>
&#13;
&#13;
&#13;