Membership
Main Menu
Forum Boards
Stats
- 18 tutorials
- 72,304 members
- 696,089 forum posts
- 11 blog posts
Tutorials
PHP Basic Pagination
Views: 45398
In this chunk of code, we make the range of page links display. The idea is that if we're on page 8, it will show:
<< < 5 6 7 [8] 9 10 11 > >>
We start with a range ($range). The range covers how many numbers to the left and to the right of the current page (not total numbers to be displayed). The range in the code is 3, so if we are on page 8, it shows 5 6 7 on the left, 8 in the middle, 9 10 11 on the right. The loop starts at the current page minus 3, and iterates to the current page plus 3.
Inside the loop, we first check to make sure that the current iteration is a valid page number, because for instance, if we are on page 1, we do not want to show links to page -2 -1 0. The next thing we do is check to see if the current iteration of the loop is on the current page. If it is, we want to 'highlight' it by making it bold with brackets around it to make it stand out a little. We also do not want to make it a link, because it's just silly to make a link to the same page you are on. I can just imagine poorly written page raker or spider bots getting stuck in an infinite loop from that, which is kinda funny, except it's your bandwidth they are eating up.
Anyways, if it's a valid page number, and it's not the current page, then make a link of that page number. The end.
