如何一次获取一页的数据


How to fetch the data for one page at a time

我在使用Bootstrap的jQuery DataTables中一次获取行数的问题。首先,我下面的代码从数据库中获取整个数据并开始分页。

但是我希望一次加载一页的数据。

我的PHP代码是:

<table id="example1" class="table table-bordered table-striped">
    <thead>
    <tr>
    <th> Name</th>
    <th>category</th>
    <th>Location</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach ($datastat as $row) {
    ?>
    <tr>
    <td><?php echo $row['fname'];?> <?php echo $row['mname'];?> <?php echo $row['lname'];?></a></td>
    <td><?php echo $row['category'];?></td>
    <td><?php echo $row['location'];?></td>
    </tr>
    <?php }?>
    </tbody>
    <tfoot>
    <tr>
    <th> Name</th>
    <th>category</th>
    <th>Location</th>
    </tr>
    </tfoot>
    </table>     

我的JavaScript代码是:

 $(function() {
    $('#example1').dataTable({
       "bPaginate": true,
       "bLengthChange": true,
       "bFilter": true,
       "bSort": true,
       "pageLength": 10
    });
 });

您需要实现服务器端处理才能一次从服务器检索一页,请参阅此示例。

幸运的是,DataTables发行版包括PHP类ssp.class.php和示例代码,使其相当容易与PHP一起使用。