Pagination function


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //displays the pagination
  2. function pagination($total_or_query, $thispage='', $url='', $type='database', $class='', $per_page_overwrite=''){
  3. global $site;
  4. $per_page = $site['perPage'];
  5. if($per_page_overwrite) $per_page=$per_page_overwrite;
  6. $links='<div class="pagination">';
  7.  
  8. //returns the no of entries in table
  9. if($type=='database') $r=mysql_num_rows(mysql_query($query));
  10. else $r=$total;
  11.  
  12. if($type=='database' && !$r){
  13. trigger_error('Pagination Database Error<br /><small class="verysmall">Technical Message: '.mysql_error().'</small>', E_USER_ERROR);
  14. }elseif($type!='database' && !$r){
  15. return false;
  16. }else{
  17. $n=ceil($r/$per_page); //total number of pages
  18. $previouspage=$thispage-1;
  19. $nextpage=$thispage+1;
  20. $maxCurrentPage = 7;
  21.  
  22. if($previouspage<=$n && $previouspage>=1){
  23. $links.='<a href="'.$url.'1" title="First Page" class="'.$class.'">&laquo; First</a> <a href="'.$url.$previouspage.'" title="Previous Page" class="'.$class.'">&laquo; Previous</a>';
  24. }
  25.  
  26. if( $thispage <= $n ){
  27. if( $thispage <= 3 ){
  28. $max = ( $n > $maxCurrentPage ) ? $maxCurrentPage : $n;
  29. for($i = 1; $i<=$max; $i++){
  30. $links .= ( $i == $thispage ) ? '<a href="'.$url.$i.'" class="number current '.$class.'" title="Page '.$i.'">'.$i.'</a>' : '<a href="'.$url.$i.'" class="number '.$class.'" title="Page '.$i.'">'.$i.'</a>';
  31. }
  32. }else{
  33. for($i = ($thispage-3); $i<=($thispage+3); $i++){
  34. if( $i <= $n ){
  35. $links .= ( $i == $thispage ) ? '<a href="'.$url.$i.'" class="number current '.$class.'" title="Page '.$i.'">'.$i.'</a>' : '<a href="'.$url.$i.'" class="number '.$class.'" title="Page '.$i.'">'.$i.'</a>';
  36. }
  37. }
  38. }
  39. }
  40.  
  41. if($nextpage<=$n){
  42. $links.='<a href="'.$url.$nextpage.'" title="Next Page" class="'.$class.'">Next &raquo;</a> <a href="'.$url.$n.'" title="Last Page" class="'.$class.'">Last &raquo;</a>';
  43. }
  44.  
  45. }
  46.  
  47. $links.='</div>';
  48. return $links;
  49. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.