我正在使用以下代码将MySQL数据加载到分页的html表中:
<table data-paginate="true">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT column_1, column_2, column_3 FROM table";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["column_1"] . "</td><td>" . $row["column_2"] . "</td><td>" . $row["column_3"] . "</td></tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
</tbody>
</table>
当涉及到处理大量数据时,这种方法不是很实用。将MySQL查询的加载推迟到用户单击表的下一页的正确方法是什么?
您不会延迟,即将结果存储在某个地方,然后重用它。
您可以修改您的查询以选择正确的10行,例如,在每次迭代中,您的分页(如)
首页:
SELECT column_1, column_2, column_3 FROM table LIMIT 0,10
第二页:
SELECT column_1, column_2, column_3 FROM table LIMIT 9,10
第三页:
SELECT column_1, column_2, column_3 FROM table LIMIT 19,10
然后,您只需要存储$rows_per_page
和$current_page
,每次表单向脚本发送page_number、page_plus_1或page_minus_1时,您都可以从中计算LIMIT值。
此处需要分页。寻呼本身有两种类型的
1) server side
On clicking the next button, it re-queries and brings the data
2) client side
Loads whole of the data in one go but uses jquery to show it in divisions.
不要重新发明轮子,使用现有的apihttps://www.datatables.net/.它同时具有这两种功能,并且还支持导出功能。