This shows how to page thru a large resultset in a simple manner. It produces a set of links where-ever its called that looks like:
[ << 1 2 3 4 >> ]
and so on.
Bastien
<?php
/*
show_pages shows the links to the different pages
it gets passed the current offset and the total number of images to
show on one page ($display_length)
*/
function show_pages($offset, $display_length)
{
//run a query to see how many records there are total.
$total = ''; //resource handle for the query
$total_images = 0; //total number of elements in the paging system
$pages = 0; //number of pages calculated in the script
$base_offset = 0; //starting point for offsets
$show_pages = 3; //number of links to show as pages
$start_page = 1; //index number of the page to start with
echo "<table width='100%'><tr><td align='center'><b>[ ";
$sql = "select count(*) from photos";
$total = query($sql);
if (mysql_num_rows($total)==1)
{
$row = mysql_fetch_array($total);
$total_images = $row[0];
$pages = floor($total_images/$display_length); //calc the number of links to show (ceil rounds up to next int)
if ($pages > $show_pages)
{
$base_offset = $offset - $display_length;
//can't go past first page
if ($base_offset < 0){ $base_offset = 0; }
$start_page = $base_offset / $display_length;
$end_page = $show_pages + $start_page;
//can't go past last page
if ($end_page > $pages) { $end_page = $pages; }
}//end if
if ($start_page > 1)
{
echo "<a href=\"".$_SERVER['PHP_SELF']."?offset=".($base_offset-$display_length)."\"><<</a> ";
}
for ($x = $start_page ; $x <= $end_page; $x++)
{
//don't show current page as link
if ($base_offset == $offset)
{
echo "$x ";
}else{
echo "<a href=\"".$_SERVER['PHP_SELF']."?offset=$base_offset\">$x</a> ";
}//end if
$base_offset += $display_length;
}//next
if ($pages > $end_page)
{
echo "<a href=\"".$_SERVER['PHP_SELF']."?offset=$base_offset\">>></a> ";
}
}else{
echo "No other images found";
}
echo "]</b></td></tr></table>";
}//end function
?>