You are seeing this very light version of the page, as you are using an outdated
browser!
Onlinetools.org is done in XHTML 1.0 and supports CSS 2.0, please upgrade your
browser if you want a better version of this site.
Thank you, please help us developers keep up with the industry rather than patching our code for outdated applications.
Two PHP functions do most of the work for you: foreach(arrayName) and array_slice(arrayName,start,number).
foreach(arrayName) displays all the elements in arrayName, array_slice() extracts the elements between start and start+number from arrayName.
array_slice() also checks if start+number exceeds the amount of elements in the array, and, if that is the case, only returns the remaining bits.
Enough theory, on with the code.
For testing this idea, let's define an array "entries" with the numbers from 0 to 24, and display 5 numbers at a time.
If there is no display-start set, let 0 be the first item to be displayed. The last item displayed on this page should be the 5th after start.
Now for the display the previous link. First make sure, that a previous link is needed. A previous link is needed on each page that displays elements after the 5th element. So, all you need to do is checking if the first element on this page is the 5th or after the 5th.
Then make the link to link to a list, that starts 5 elements before the current first element.
For the URL, use $PHP_SELF, which allows the links to be used in any file or even as an include.
Now display the first element and the 4 following ones, using array_slice() and foreach() to extract the elements and display them. Using these functions means you don't have to worry about exceeding the number of available elements while trying to display them.
Finally check if you need a "next" link. Each page but the last one should have one, so compare the last of the displayed elements with the number of available elements. If the end of this page's list is not yet the last element of the full list or even higher than the number of availabe elements, don't display the link.
Make the link link to a list that starts with an element which is 5 elements after the current first element.
And that is all it takes to create fully functional "previous" and "next" links.
Table of contents:
The issue with Previous and Next links
PHP to the rescue
The whole script