繁体   English   中英

使用php从mysql表中排序数据

[英]sort data from mysql table using 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>

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

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

默认排序是升序,否则可以降序

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

为什么不使用order by子句:

SELECT * FROM table ORDER BY列;

按参考顺序

如果可以使用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> 

暂无
暂无

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

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