计算 for 循环的偏移量
我正在尝试对我得到的数组进行分页,目前我正在使用像这样的 for 循环
for($i = $pages->low;$i<$total;++$i )
我需要弄清楚的是如何根据当前页面和行数计算 $total
变量,以便循环能够正确计算项目数量在数组中。
我有以下变量:
$pages->low (equals the number of rows the pagination has already been through
e.g. Page 1 = 0, Page 2 = 5, Page 3 = 10 etc...
$pages->total_items (explains itself)
$pages->current_page
$pages->ipp (items per page, FYI 5)
那么我将使用什么公式来计算循环应经过的行数,例如,如果数组中总共有 13 个项目,并且每页有 5 个结果,在第一页 $total
应该等于 5,第二页应该等于 10,第三页应该等于 13 等等?
谢谢
I'm trying to do pagination on an array that ive got and currently im looping through it with a for loop like this
for($i = $pages->low;$i<$total;++$i)
What I need to figure out is how to get the $total
variable to an be calculated based on the current page and the count of rows so the loop works correctly for the amount of items in the array.
I've got the following variables:
$pages->low (equals the number of rows the pagination has already been through
e.g. Page 1 = 0, Page 2 = 5, Page 3 = 10 etc...
$pages->total_items (explains itself)
$pages->current_page
$pages->ipp (items per page, FYI 5)
So what formula would I use to calculate the amount of rows the loop should go through so for example if there was 13 items in total in the array and 5 results per page, on page one $total
should equal 5, page two should equal 10 and page three should equal 13 etc?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它的作用显而易见,但限制了项目的总数。
虽然我个人会简单地在这里使用
LimitIterator
。It does the obivous, but limits it the the total number of items.
Though I personally would simply use a
LimitIterator
here.来自 Kohana 的分页模块:
From Kohana's pagination module:
不清楚,为什么如果第一页有 13 项,总数应该等于 5 ???
对我来说,如果您尝试在第 2 页上显示 $pages->ipp 下一个项目,只需从 $pages->low 到 $pages- >低 + $pages->ipp
not clear, why if there was 13 items on page one total should be equal to 5 ???
For me if you are trying to show the $pages->ipp next items on pages 2 juste go from $pages->low to $pages->low + $pages->ipp