2011-02-03 193 views
0

我尝试这样做,香港专业教育学院得到了目前即时通讯通过它与一个循环的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条结果,第1页$total应该等于5,第2页应该等于10,第3页应该等于13等?

感谢

+0

那是小学数学。你确定你可以在没有基本算术的情况下编程吗? – 2011-02-03 14:26:38

回答

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

它做的obivous,但限制它的项目的总数。

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

+0

这是完美的(LimitIterator),比我之前使用的延迟循环要好得多。 – Jack 2011-02-03 14:33:57

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); 
0

尚不清楚,为什么,如果有第13名的项目之一总额应等于5 ???

对于我来说,如果你想显示在两页$ pages-> IPP下一个项目从$ pages->低中庸之道去$ pages->低+ $ pages-> IPP