如何从mysql数据库中过滤搜索数据并将其显示在数据表中?

时间:2017-05-15 13:10:18

标签: php jquery json ajax mysqli

我正在使用数据表来显示数据。我有大型数据库近15k行,加载数据需要时间,所以我在数据表中只显示200行。但是数据表中的搜索过滤器是这里需要的最重要的事情。因此我需要通过php通过对keyup事件的ajax调用直接从数据库中搜索数据。那我该怎么做呢?

这是我在这里所做的事情。

    <!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Trail 2</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="datatable/css/jquery.dataTables.min.css" rel="stylesheet">
    <!--<link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap.min.css" rel="stylesheet">-->

    <style>
        tfoot input {
            width: 100%;
            padding: 3px;
            box-sizing: border-box;
        }
    </style>

  </head>
  <body>
    <br><br>
        <div class="container">
            <div class="row">
                <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Surname</th>
                            <th>City</th>
                            <th>Mobile No</th>
                        </tr>
                    </thead>
                    <tfoot>
                        <tr>
                            <th><input type="text" name="name" id="name" placeholder="Search by NAME"></th>
                            <th>Surname</th>
                            <th>City</th>
                            <th>Mobile No</th>
                        </tr>
                    </tfoot>
                    <tbody>
                    <?php 
                    require('connectivity2.php');

                    $query1 = "SELECT * FROM member LIMIT 200;";
                    $result1 = mysqli_query($con, $query1);

                    while($row = mysqli_fetch_assoc($result1)){
                        $name = $row['m_name'];
                        $surname = $row['m_sec_surname'];
                        $city = $row['m_city'];
                        $no = $row['m_mobile'];
                    ?>  
                        <tr>
                            <td><?php echo $name; ?></td>
                            <td><?php echo $surname; ?></td>
                            <td><?php echo $city; ?></td>
                            <td><?php echo $no; ?></td>
                        </tr>
                <?php   }  //End of while loop
                ?>      
                    </tbody>    
                </table>
            </div>
        </div>
    <script src="js/jquery-1.12.4.js"></script>
    <script src="datatable/js/jquery.dataTables.min.js"></script>
    <!--<script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap.min.js"></script> -->

    <script type="text/javascript">
        $(document).ready(function() {

            // DataTable
            var table = $('#example').DataTable();

            // Search
            $("#name").keyup(function(){
                var m_name = $("input[name='name']").val();
                //window.alert(m_name);
                $.ajax({
                    type: 'POST',
                    url: 'trial2php.php',
                    data: {member name:m_name},
                    cache: true,
                    datatype: 'html',
                    success: function(){
                        $('#example').DataTable();
                    }
                });
            });

        } );
    </script>
  </body>
</html>

这里我只在表格的页脚中输入了一个输入文本来测试搜索。

ajax功能有问题。请帮帮我。

2 个答案:

答案 0 :(得分:0)

仅选择所需的列:

SELECT ItemCode, ItemName, RackNo, ItemSrNo

为列创建索引:

ItemCode, ItemName, RackNo, ItemSrNo

请参阅: https://dev.mysql.com/doc/refman/5.7/en/create-index.html

答案 1 :(得分:0)

您应该使用DataTables的服务器端处理和管道功能。这种方式在DataTables中获取,渲染和过滤数据将是顺利的。

点击以下链接,通过示例了解有关该流程的更多信息 -

DataTables Server Side Processing with Pipeline