推迟在HTML表中加载MySQL结果的正确方法是什么?

时间:2015-12-17 12:27:38

标签: php mysql

我使用以下代码将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查询的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

你不推迟,即将结果存储在某处,然后重复使用它。

您修改查询以选择正确的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值。< / p>

答案 1 :(得分:0)

你需要分页。 分页本身有两种类型

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.

不要重新发明轮子,使用像https://www.datatables.net/这样的现有api。 它有两个,它也支持导出功能。