pagination

Calculating item offset for pagination


I am trying to implement pagination and will need to calculate the item offset to use in limiting my result set. I am having problems calculating what index the first item of the page should have.

eg.

with 1 page having 10 items

page 1 will have items 1 - 10
page 2 ............... 11 - 20
page 3 ............... 21 - 30

I thought of

offset = page * itemsPerPage + 1

But this will not be the case for page 1. there must be a formula for this? I am using PHP/Zend_Paginator/Doctrine2 but this should be language independent.


Solution

  • Use offset = (page - 1) * itemsPerPage + 1.