Calculating item offset for pagination

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


Honestly depends. I'm no PHP person, but I'll put both out there. If you are pulling your records in to some form of collection (list, array, etc.) then your formula should be:

offset = (page - 1) * itemsPerPage

This is because most (again, I'm not a PHP person) arrays and lists use 0 for their first element. If they do not use 0 for their first element and/or you're pulling from a table or something else where the IDs start at 1, then it should be:

offset = (page - 1) * itemsPerPage + 1

I hope that's clear and helps.

Tags:

Pagination