9

例如,假设:

$startIndex = 21; 
$totalItems = 100;
$itemsPerPage = 10;
$totalPages = ceil($totalItems / $itemsPerPage);  // (10)

$currentpage = //I am stumped here.

根据 $startIndex,$currentpage 会是什么?

我正在研究分页,它正在踢我的屁股。我知道这可能是一些非常简单的数学,但我现在想不出来。

4

4 回答 4

15

每页 10 个项目,假设您的项目计数/编号从 1 开始,第 1 页包含项目 1 到 10,第 2 页包含项目 11 到 20,第 3 页包含 21 到 30,依此类推。

所以,

$currentPage = ceil(($startIndex - 1) / $itemsPerPage) + 1;

我曾经ceil()确保你有一个整数,它被四舍五入,所以当前的页码是正确的。


如果您的项目计数从 0 开始,因此第 1 页包含项目 0 到 9,您可以跳过公式的- 1+ 1部分:

$currentPage = ceil(($startIndex) / $itemsPerPage);
于 2013-01-30T21:19:38.320 回答
8

你在数学课上睡了很多,不是吗?

$currentpage = (($startIndex - 1) / $itemsPerPage) + 1;
于 2013-01-30T21:19:40.943 回答
1

如果:

$startIndex = 21; 
$totalItems = 100;
$itemsPerPage = 10;
$totalPages = ($totalItems / $itemsPerPage);  // (10)

然后

$currentpage = $startIndex/$itemsPerPage; //Make sure that it uses integer division
于 2013-01-30T21:20:34.937 回答
0

floor用于获取当前页面很重要

pagesTotal = int(ceil(count / limit))
pagesCurrent = int(floor(offset / limit) + 1)
于 2020-02-26T09:06:19.327 回答