2011-02-03 5 views
0

私が得たIVEと、現在私が把握する必要があり何このはループ

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の項目が配列であった場合、ループは例えばので、通過する必要がある行の量を計算するためにどのような式を使用すると、 1ページにつき5件の結果、$totalは5、2ページは10、3ページは13等となるはずですか?

おかげ

+0

thats小学校の数学。あなたは基本的な算術を使わずにプログラムできると確信していますか? –

答えて

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

それはobivousを行いますが、それは項目の合計数を制限します。

私は個人的にはここではLimitIteratorを使用していますが、 Kohanaのページネーションのモジュールから

+0

これは完璧(LimitIterator)で、私が以前使っていた減速したforループよりはるかに優れています。 – Jack

0
$start_from = ($current_page - 1) * $per_page; 

:ページ上の13個の項目があった場合

$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

明確ではない、なぜ1つの合計が5に等しくなければなりません?私にとって

あなたはジュスト

IPP $ pages->低+ $ pages->に$ pages->低から行くページ2の$ pages-> IPPの次の項目を表示しようとしている場合