Codesnipp.it Social Code Sharing

Wayne Pincence

PHP Paging Algorithm

by Wayne Pincence on Aug 21, 2010

/* $current_page=$_REQUEST['page']; $total_pages=120; */ $grace=5; // 5 pages on the left and 5 pages on the right of current page $range=$grace*2; $start = ($current_page - $grace) > 0 ? ($current_page - $grace) : 1; $end=$start + $range; if($end > $total_pages){ //make sure $end doesn't go beyond total pages $end=$total_pages; $start= ($end - $range) > 0 ? ($end - $range) : 1; //if there is a change in $end, adjust $start again } if($start>1){ echo "1 ..."; } for($i=$start;$i<=$end;$i++){ if($i==$current_page){ echo "$i "; // Current page is not clickable and different from other pages } else { echo "$i "; } } if($end < $total_pages){ echo "... $total_pages"; // If $end is away from total pages, add a link of the last page } ?&gt;

Can't see the comments? Please login first :)