0

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

1
  • thats elementary school math. Are you sure you'll be able to program without basic arithmetics? Feb 3, 2011 at 14:26

3 Answers 3

1
$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.

1
  • That's perfect (the LimitIterator), much better than the retarded for loop I was using before.
    – Jack
    Feb 3, 2011 at 14:33
0
$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);
0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.