简体   繁体   English

通过使用PHP计算行数和每页预定义结果(rpp)的动态分页

[英]Dynamic pagination by counting the number of rows and a pre-defined result per page (rpp) with PHP

I whipped up some code to count the number of rows from my database, and through a little script, display "Page : 1 2 3 4 5" etc. 我整理了一些代码以计算数据库中的行数,并通过一个小脚本显示“ Page:1 2 3 4 5”等。

Here is my code: 这是我的代码:

$totalRows = mysql_num_rows
$rpp = 20
$totalPages = ceil($totalRows/$rpp);
$i;

for (i=0; i<totalPages; i++){
    echo "Page: " . "<a href='index.php?page=\"$i\"rpp=20>\"$i\"</a>";
}

Does this look good? 这样看起来好吗? Do I need anything else? 我还需要其他东西吗?

EDIT 1: Added ceil() to round up. 编辑1:添加了ceil()进行舍入。 No more missing results :-)! 没有更多的丢失结果了:-)!

I think you need to round $totalPages to the next highest integer, ie. 我认为您需要将$ totalPages舍入到下一个最大整数,即。

$totalPages = ceil($totalRows/$rpp); $ totalPages = ceil($ totalRows / $ rpp);

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

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