page_list (With HTML output)


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



Copy this code and paste it in your HTML
  1. function page_list($link, $page, $per_page, $total_rows, $options = array()){
  2.  
  3. $link = urldecode($link);
  4.  
  5. $default_options = array(
  6. 'paging_range' => 2,
  7. 'first_last_link' => true,
  8. 'first_character' => '<em>&laquo;</em> First',
  9. 'last_character' => 'Last <em>&raquo;</em>',
  10. );
  11.  
  12. $options = array_merge_replace($default_options, $options);
  13.  
  14. $total_pages = ceil($total_rows / $per_page);
  15. $record_start = $page > 1 ? ($per_page * ($page-1)) : 0;
  16. $record_end = ($record_start + $per_page) > $total_rows ? $total_rows : ($record_start + $per_page);
  17.  
  18. $page_list = array();
  19.  
  20. if($total_pages > 1){
  21.  
  22. $first_display_page = $page - $default_options['paging_range'];
  23. $first_display_page = $first_display_page < 1 ? 1 : $first_display_page;
  24. $last_display_page = $page + $default_options['paging_range'];
  25. $last_display_page = $last_display_page > $total_pages ? $total_pages : $last_display_page;
  26.  
  27. for($p = $first_display_page; $p <= $last_display_page; $p++){
  28. $page_data = array(
  29. 'page' => $p,
  30. 'text' => $p,
  31. 'link' => ''
  32. );
  33.  
  34.  
  35. if($options['first_last_link'] === true && $first_display_page === $p && $first_display_page > 1){
  36. $page_data['text'] = $default_options['first_character'];
  37. $page_data['page'] = 1;
  38. }elseif($options['first_last_link'] === true && $last_display_page === $p && $total_pages > $p){
  39. $page_data['text'] = $default_options['last_character'];
  40. $page_data['page'] = $total_pages;
  41. }
  42.  
  43. $page_data['link'] = str_replace('{page}', $page_data['page'], $link);
  44.  
  45. $page_list[] = $page_data;
  46.  
  47. }
  48.  
  49.  
  50. }
  51.  
  52. $pages = '<p class="text">Page '.number_format($page).' of '.number_format($total_pages).'</p>';
  53.  
  54. if(count($page_list) > 0){
  55. $pages .= '<ul class="list">';
  56. foreach($page_list as $single_page){
  57. $pages .= '<li class="'.($single_page['page'] == $page ? ' selected' : false ).'"><span><a href="'.$single_page['link'].'">'.$single_page['text'].'</a></span></li>';
  58. }
  59. $pages .= '</ul>';
  60. }
  61.  
  62.  
  63. return $pages;
  64.  
  65. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.