简体   繁体   English

如何在分页中显示很长的for / for循环?

[英]How to display long while/for loop in pagination?

I have a while/for loop with thousands results. 我有一个while / for循环,有成千上万的结果。 Since it contains several fields, I use table to display it. 由于它包含几个字段,因此我使用表来显示它。 However, with thousands results being displayed at the same time, it will definitely slow down the performance. 但是,由于同时显示数千个结果,因此肯定会降低性能。 How can I display it in pagination? 如何在分页中显示它?

    <table>
    <?php $count = 1000;
    for($a=1;$a<=$count;$a++) { ?>
        <tr>
            <td><?php echo $a; ?></td>
            <td>This is number <?php echo $a; ?></td>
        </tr>
    <?php $i++; 
    } ?>
    </table>

If the data comes from a database you can limit the result set. 如果数据来自数据库,则可以限制结果集。 If you are using MySQL you use the LIMIT clause. 如果使用MySQL,则使用LIMIT子句。 This will allow you to only fetch the specified number of rows. 这将只允许您获取指定的行数。

SELECT title, description FROM posts LIMIT 25

This will only fetch 25 rows. 这只会获取25行。 Then if you want to fetch the results after row 25 you can provide an offset. 然后,如果要在第25行之后获取结果,则可以提供一个偏移量。 This is done a little different since the offset comes first in the LIMIT clause. 由于偏移量在LIMIT子句中排在第一位,因此这样做有所不同。 Only one argument is provided MySQL assumes its the limit instead. 仅提供了一个参数,而MySQL假定其为极限。 To select the next 50 rows you use. 要选择接下来的50行,请使用。

SELECT title, description FROM posts LIMIT 25, 50

This can be useful to reduce the result set fetched and help increase performance/load times due to a smaller amount of data that needs to be processed. 由于减少了需要处理的数据量,因此这对于减少获取的结果集并帮助增加性能/加载时间很有用。

I hope this can help you, happy coding! 希望对您有所帮助,编码愉快!

Update 更新资料

This is a little tutorial in using the LIMIT clause in MySQL 这是一个在MySQL中使用LIMIT子句的小教程

My only solution without having a DB with the data would be to pass the array key you are on to the next page. 我唯一没有数据的数据库的解决方案是将您要使用的数组键传递到下一页。 For example: 例如:

$start = 1;
$end = 1000;
if(isset($_GET['record_number'])){
    $start = $_GET['record_number'];
    $end = $_GET['record_number'] + 1000;
}

for($a=$start; $a<=$end; $a++){
    //code here
}

Other than that, you might consider creating a list of files in a DB engine so you can use the LIMIT parameter. 除此之外,您可能会考虑在数据库引擎中创建文件列表,以便可以使用LIMIT参数。

Here is my example, it's similar to the answer from @AnotherGuy 这是我的示例,类似于@AnotherGuy的回答

<form method="GET">
<input type="text" name="rowsPerPage">
<?php
    for($i = 1; $i+1 < $count/$rowsPerPage; $i++){
        echo '<input type="submit" name="page" value="'.$i.'">';
    }
?>
</form>


<?php
    $begin = (int)$_GET['rowsPerPage']*$_GET['page'];
    $take = (int)$_GET['rowsPerPage'];
    $sql = "SELECT ... LIMIT {$begin}, {$take}";
?>

It's possible that the code contains "typos", but I hope it will give you new ideas. 该代码中可能包含“ typos”,但我希望它能为您带来新的想法。

I would recommend you to use GET instead of POST. 我建议您使用GET而不是POST。 GET will be saved in the URL, in this way, it will be easier to reload the page without losing the page-settings. GET将保存在URL中,这样,在不丢失页面设置的情况下,可以更轻松地重新加载页面。

www.example.com?rowsPerPage=150&page=2 www.example.com?rowsPerPage=150&page=2

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

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