我正在尝试对已经获得的数组进行分页,并且目前正在使用这样的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等?
由于
答案 0 :(得分:1)
$total = min($pages->ipp * ($pages->current_page + 1), $pages->total_items);
显而易见,但限制了项目的总数。
虽然我个人只会在这里使用LimitIterator
。
答案 1 :(得分:0)
$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);
答案 2 :(得分:0)
不清楚,为什么如果页面上有13个项目总共应该等于5 ???
对我来说,如果你想在第2页上显示 $ pages-&gt; ipp 下一个项目,请从 $ pages-&gt; low 转到 $ pages-&gt;低+ $ pages-&gt; ipp