Index ¦ Archives ¦ Atom

Pagination Formula

Pagination forumla

Last Result Number

The last item on a page is the page offset multipled by the length of a page. For example, if we're on page 1 of a page with 5 items per page, the last item on the page is item 5, or offset (1) * page length (5) = 5.

Last result number (end) = page offset * length

Starting Result Number

The starting result is achieved by taking the page offset multiplied by the length of a page and subtracting either the length of a page - 1 or the last item on a page + 1 minus the length of the page.

Start result number (start) = (page offset * page length) - (length - 1)

or

Start result number (start) = (page offset * page length) - last/end + 1 - length

And redefined without using the last result number:

Start result number (start) = (page offset * (page length - 1)) + 1

I wrote this formula down thinking I'd use it a bit and hopefully next time I need pagination that I create on my own I can use what I've figured out here. If not, maybe it can help someone else. Oh, and there might be bugs in these calculations or unexpected results that need to be tested.

© Steve Spigarelli. Built using Pelican. Theme by Giulio Fidente on github.