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.

open or close right navigation

Previous and Next links demystified

Advertisement:
 
written by:Christian Heilmann on 12.05.2002
Jump to page: 1  2  3  or read all on one page
 

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.

$i=0;
while ($i<=24){
    $entries[]=$i;
    $i++;
    }
$increase=5;    

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.

if(!$start){$start=0;}
$end=$start+$increase;

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.

$disp=array_slice($entries,$start,$increase);
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.

more»
Table of contents:

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

Back to top

Search:

 

Font:

Click to decrease fontsizeClick to increase fontsize

Text-colour:


Background:


sponsored by :

NWU

Help Onlinetools.org

Help onlinetools.org, donate via paypal

Partners

easyPHP

icons

Link to us

copy the code here:


Powered by EasyCMS