计算 for 循环的偏移量

发布于 2024-10-16 03:04:41 字数 609 浏览 3 评论 0原文

我正在尝试对我得到的数组进行分页,目前我正在使用像这样的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

郁金香雨 2024-10-23 03:04:42
$total = min($pages->ipp * ($pages->current_page + 1), $pages->total_items);

它的作用显而易见,但限制了项目的总数。

虽然我个人会简单地在这里使用 LimitIterator

$total = min($pages->ipp * ($pages->current_page + 1), $pages->total_items);

It does the obivous, but limits it the the total number of items.

Though I personally would simply use a LimitIterator here.

自由如风 2024-10-23 03:04:42
$start_from = ($current_page - 1) * $per_page;

来自 Kohana 的分页模块:

$this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
$this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
$this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
$this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
$this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
$this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
$this->first_page         = ($this->current_page === 1) ? FALSE : 1;
$this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
$this->offset             = (int) (($this->current_page - 1) * $this->items_per_page);
$start_from = ($current_page - 1) * $per_page;

From Kohana's pagination module:

$this->total_pages        = (int) ceil($this->total_items / $this->items_per_page);
$this->current_page       = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
$this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
$this->current_last_item  = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
$this->previous_page      = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
$this->next_page          = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
$this->first_page         = ($this->current_page === 1) ? FALSE : 1;
$this->last_page          = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
$this->offset             = (int) (($this->current_page - 1) * $this->items_per_page);
屋檐 2024-10-23 03:04:42

不清楚,为什么如果第一页有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文