Php 计算for循环的偏移量

Php 计算for循环的偏移量,php,loops,pagination,for-loop,offset,Php,Loops,Pagination,For Loop,Offset,我正在尝试对我得到的一个数组进行分页,目前我正在使用类似这样的for循环遍历它 for$i=$pages->low;$i 它会发出讣告,但会限制物品的总数 虽然我个人会在这里简单地使用 从Kohana的分页模块: $this->total_pages = (int) ceil($this->total_items / $this->items_per_page); $this->current_page = (int) min(max(1, $t

我正在尝试对我得到的一个数组进行分页,目前我正在使用类似这样的for循环遍历它

for$i=$pages->low;$i
它会发出讣告,但会限制物品的总数

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

从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);
$this->total\u pages=(int)ceil($this->total\u items/$this->items\u per\u page);
$this->current_page=(int)min(max(1,$this->current_page),max(1,$this->total_page));
$this->current\u first\u item=(int)min(($this->current\u page-1)*$this->items\u per\u page)+1$this->total\u items);
$this->current\u last\u item=(int)min($this->current\u first\u item+$this->items\u per\u page-1,$this->total\u items);
$this->previous\u page=($this->current\u page>1)?$this->current\u page-1:FALSE;
$this->next_page=($this->current_page<$this->total_page)?$this->current_page+1:FALSE;
$this->first\u page=($this->current\u page==1)?FALSE:1;
$this->last\u page=($this->current\u page>=$this->total\u page)?FALSE:$this->total\u page;
$this->offset=(int)(($this->current\u page-1)*$this->items\u per\u page);

不清楚,为什么如果第一页上有13个项目,那么总数应该等于5


对我来说,如果你想在第2页上显示$pages->ipp下一项,只需从$pages->low转到$pages->low+$pages->ipp

,这就是小学数学。你确定你能在没有基本算术的情况下编程吗?那太完美了(极限迭代器),比我以前使用的延迟for循环要好得多。
$total = min($pages->ipp * ($pages->current_page + 1), $pages->total_items);
$start_from = ($current_page - 1) * $per_page;
$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);