Previous and Next links demystified
Advertisement:
written by:Christian Heilmann on 12.05.2002
The whole script
Here's the whole script for copy and paste.
<?php
$start=$HTTP_GET_VARS['start'];
// Populate the demo array
$i=0;while ($i<=24){$entries[]=$i;$i++;}
// the number of items displayed on one screen.
$increase=5;
// if there is no start value set, set start to 0
if(!$start){$start=0;}
// end of display is start plus increase
$end=$start+$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> ";
}
// take the part of the entries in between start and start*increase and display it
$disp=array_slice($entries,$start,$increase);
foreach ($disp as $d){echo " $d ";}
// 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 " <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.
Table of contents:
The issue with Previous and Next links
PHP to the rescue
The whole script