Advertisement
Guest User

Untitled

a guest
May 1st, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | None | 0 0
  1. class Pagination {
  2.     public $total = 0;
  3.     public $page = 1;
  4.     public $limit = 20;
  5.     public $num_links = 8;
  6.     public $url = '';
  7.     public $text_first = '|<';
  8.     public $text_last = '>|';
  9.     public $text_next = '>';
  10.     public $text_prev = '<';
  11.  
  12.     public function render() {
  13.         $total = $this->total;
  14.  
  15.         if ($this->page < 1) {
  16.             $page = 1;
  17.         } else {
  18.             $page = $this->page;
  19.         }
  20.  
  21.         if (!(int)$this->limit) {
  22.             $limit = 10;
  23.         } else {
  24.             $limit = $this->limit;
  25.         }
  26.  
  27.         $num_links = $this->num_links;
  28.         $num_pages = ceil($total / $limit);
  29.  
  30.         $this->url = str_replace('%7Bpage%7D', '{page}', $this->url);
  31.  
  32.         $output = '<ul class="pagination">';
  33.  
  34.         if ($page > 1) {
  35.             $output .= '<li><a href="' . str_replace('{page}', 1, $this->url) . '">' . $this->text_first . '</a></li>';
  36.             $output .= '<li><a href="' . str_replace('{page}', $page - 1, $this->url) . '">' . $this->text_prev . '</a></li>';
  37.         }
  38.  
  39.         if ($num_pages > 1) {
  40.             if ($num_pages <= $num_links) {
  41.                 $start = 1;
  42.                 $end = $num_pages;
  43.             } else {
  44.                 $start = $page - floor($num_links / 2);
  45.                 $end = $page + floor($num_links / 2);
  46.  
  47.                 if ($start < 1) {
  48.                     $end += abs($start) + 1;
  49.                     $start = 1;
  50.                 }
  51.  
  52.                 if ($end > $num_pages) {
  53.                     $start -= ($end - $num_pages);
  54.                     $end = $num_pages;
  55.                 }
  56.             }
  57.  
  58.             for ($i = $start; $i <= $end; $i++) {
  59.                 if ($page == $i) {
  60.                     $output .= '<li class="active"><span>' . $i . '</span></li>';
  61.                 } else {
  62.                     $output .= '<li><a href="' . str_replace('{page}', $i, $this->url) . '">' . $i . '</a></li>';
  63.                 }
  64.             }
  65.         }
  66.  
  67.         if ($page < $num_pages) {
  68.             $output .= '<li><a href="' . str_replace('{page}', $page + 1, $this->url) . '">' . $this->text_next . '</a></li>';
  69.             $output .= '<li><a href="' . str_replace('{page}', $num_pages, $this->url) . '">' . $this->text_last . '</a></li>';
  70.         }
  71.  
  72.         $output .= '</ul>';
  73.  
  74.         if ($num_pages > 1) {
  75.             return $output;
  76.         } else {
  77.             return '';
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement