You are seeing this very light version of the page, as you are using an outdated browser! 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.

open or close right navigation

Previous and Next links demystified

written by:Christian Heilmann on 12.05.2002
Table of contents:

The issue with Previous and Next links
PHP to the rescue
The whole script


The issue with Previous and Next links

"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.

  1. Define how many elements to display on a page.
  2. IF you are NOT on the first page, display the "previous" link, which sets the start of the display to the current start minus the defined number of elements.
  3. Display the amount of elements defined beforehand, making sure that there are enough to display on the last page.
  4. IF there are still more elements in the list to display, define that this is not the last page.
  5. display the next link, which increases the start of the display by the defined number, unless you are on the last page

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.

PHP to the rescue

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.

while ($i<=24){

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.

if ($start >= $increase){
    echo "<a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?start=".($start-$increase)."\">previous</a>&#160;";

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.

foreach ($disp as $d){echo "&#160;$d&#160;";}

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.

if ($end < sizeof($entries)){
    echo "&#160;<a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?&start=".($start+$increase)."\">next</a>";

And that is all it takes to create fully functional "previous" and "next" links.

The whole script

Here's the whole script for copy and paste.

    // Populate the demo array
    $i=0;while ($i<=24){$entries[]=$i;$i++;}

    // the number of items displayed on one screen.
    // if there is no start value set, set start to 0
    // end of display is start plus increase
    // If start is equal or bigger than increase, display the previous
    // link, which sets start to start-increase
    if ($start >= $increase){
    echo "<a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?start=".($start-$increase)."\">previous</a>&#160;";
    // take the part of the entries in between start and start*increase and display it
    foreach ($disp as $d){echo "&#160;$d&#160;";}

    // If the end is smaller than the amount of items in increase, display the next
    // link, which sets start to start+increase
    if ($end < sizeof($entries)){
    echo "&#160;<a href=\"".$HTTP_SERVER_VARS['PHP_SELF']."?start=".($start+$increase)."\">next</a>";

If you are too lazy to type or copy + paste it, download this example as a zip click here.

Back to top




Click to decrease fontsizeClick to increase fontsize



sponsored by :



Help, donate via paypal




Link to us

copy the code here:

Powered by EasyCMS