10-30-2007, 07:43 PM
|
#7 (permalink)
|
|
Super Moderator
Join Date: Sep 2007
Posts: 165
Thanks: 0
|
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; }
With this function, you supply the current page number, and the total number of pages for the result set.
The function then returns an array which will be similar to this:
(If the current page is 2 and max pages = 5)
$pagination[0] = "P-1";
$pagination[1] = 1;
$pagination[2] = 2;
$pagination[3] = 3;
$pagination[4] = 4;
$pagination[5] = 5;
$pagination[6] = "N-3";
Usage example:
PHP Code:
$curr_page = 2; $total_pages = 5;
$page = pagination($curr_page, $total_pages); foreach($pages as $page) { //print $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='/".urlencode($search)."'>Prev</a>"; } elseif(stripos($page, "n-") !== false) { $p = substr($page, -1); print "<a href='/".$p."/".urlencode($search)."'>Next</a>"; } else { if($page != 1) print "<a href='/".$page."/".urlencode($search)."'>".$page."</a>"; else print "<a href='/".urlencode($search)."'>".$page."</a>"; } }
Note, i apologise for not using the standardised prefixes to variables with this example. I created it before i myself started using them.
|
|
|
|