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.
"Previous" and "next" links are one of the necessary evils any middleware developer has to script sooner or later.
Whenever you display a lot of elements of a list, like news, entries in a guestbook, or search results, it's a good idea to limit the displayed items to a certain number per page.
Not only to avoid long rendering times, but also to make it easier for the user to scan for his desired item and to prevent scrolling.
Ok, we need them, let's do them. First thing you do is to analyse the logic behind "previous" and "next" links.
There are loads of solutions for this issue, most of them using a lot of "if" and "else" statements. Especially displaying the elements while making sure that there are still some, and checking that the start of the "next" link does not exceed the number of available elements can be quite a headache.
Unless you use some of the beautiful functions of PHP that do almost all the necessary comparisons for you.
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.
Here's the whole script for copy and paste.
If you are too lazy to type or copy + paste it, download this example as a zip click here.
Link to us