Jump to content
Larry Ullman's Book Forums

Help with Limiting Pagination 10.4

Recommended Posts

I have been using the pagination script in an ecommerce site of mine, and when I started it was working great... but as the site has grown, so have the results.

I never imaged I'd have so many products and didn't really plan accordingly... I've got over 400 in one category, which gives me 20+ pages (this ends up breaking the formatting of the page).  If you wanted to limit the number of results shown, what would be the best way to accomplish this using the existing script as a base?

I'm thinking of something like....    <<  FIRST ... 4 | 5 | 6 | 7 | 8 ... LAST  >>

Instead of  PREVIOUS  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  10  |  11  |  12  |  13  |  14  |  15  |  16 |  17 |  18  |  19  |  20  NEXT

Not looking for anyone to write the code, just trying to wrap my head around how to tackle the problem and have the pages be dynamic if there is more than a given number (say 10 pages.)

Thanks as always for the amazing book!

Link to comment
Share on other sites

Thanks for the nice words! As for this situation, first of all, congrats on the growth of your site! 

I've not done this particular thing before, but my inclination is to start by thinking about what variables would exist. You'd always have $current_page and $total_pages. I think I'd then add a $show variable, for the number of additional numbered links to show (e.g., 2 gets you 4 5 6 7 8). 

Then the logic becomes:

  • If $total_pages is greater than some figure (maybe $show * 4?), don't loop through every page number. And then...
  • If there's more than $show pages between 1 and $current_page, do "FIRST...". And also...
  • If there's more than $show pages between $current_page and $total_pages, do "...LAST"

I'm not sure if that's everything but off the top of my head that should get you going in the right direction! Let us know how it goes or if you have more questions!

Link to comment
Share on other sites


  • Create New...