Heres a function a wrote a while ago, isnt the best coding but it produces some very nice results, including google like pagination!
PHP Code:
function pagination($current_page, $num_of_pages)
{
if($current_page > $num_of_pages)
$current_page = $num_of_pages;
if($current_page == "")
$current_page = 1;
if($current_page != 1)
$prev = $current_page - 1;
if($current_page != $num_of_pages)
$next = $current_page + 1;
$min = $current_page-5;
$max = $current_page+5;
if($min < 1)
$min = 1;
if($current_page < 6 && $num_of_pages < 10)
$max = $num_of_pages;
elseif($current_page < 6 && $current_page > 10)
$max = 10;
if($max > $num_of_pages)
$max = $num_of_pages;
if($prev)
$pagination[] = "P-".$prev;
for($i=$min; $i <= $max; $i++)
{
$pagination[] = $i;
}
if($next)
$pagination[] = "N-".$next;
return $pagination;
}
Usage:
I wrote the class to be standalone, thus it is a little different than if it was written within a script:
PHP Code:
$iNumPerPage = 25;
$iNumResults = 200; //number of results
$iNumPages = ceil($iNumResults/$iNumPerPage);
if($_REQUEST['iPage'] < 1)
$iPage = 1; //Page number
$aPages = pagination($iPage, $iNumPages);
print_r($aPages);
/* Outputs
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => N-2
)
*/
As you can see it produces some weird results....
If you want to parse the array, ive used code like:
PHP Code:
foreach($aPages as $page)
{
if (stripos($page, "p-") !== false)
{
$p = substr($page, -1);
if($p != 1)
print "<a href='/".$p."/".urlencode($search)."'>Prev</a>";
else
print "<a href='/'>Prev</a>";
}
elseif(stripos($page, "n-") !== false)
{
$p = substr($page, -1);
if($p != 1)
print "<a href='/".$p."/".urlencode($search)."'>Next</a>";
else
print "<a href='/'>Next</a>";
}
else
{
if($page != 1)
print "<a href='/".$page."/".urlencode($search)."'>".$page."</a>";
else
print "<a href='/".urlencode($search)."'>".$page."</a>";
}
}
Anywho, like i said before this isnt my best code, but it does work :)