Advertisement
Guest User

paginator.class.php

a guest
Sep 30th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.28 KB | None | 0 0
  1. <?php
  2.  
  3. // source: http://net.tutsplus.com/tutorials/php/how-to-paginate-data-with-php/
  4. class Paginator
  5. {
  6.     var $items_per_page;
  7.     var $items_total;
  8.     var $current_page;
  9.     var $num_pages;
  10.     var $mid_range;
  11.     var $low;
  12.     var $high;
  13.     var $limit;
  14.     var $return;
  15.     var $default_ipp = 25;
  16.     var $querystring;
  17.    
  18.     function Paginator()
  19.     {
  20.         $this->current_page   = 1;
  21.         $this->mid_range      = 7;
  22.         $this->items_per_page = (!empty($_GET['ipp'])) ? $_GET['ipp'] : $this->default_ipp;
  23.     }
  24.    
  25.     function paginate()
  26.     {
  27.         if ($_GET['ipp'] == 'All') {
  28.             $this->num_pages      = ceil($this->items_total / $this->default_ipp);
  29.             $this->items_per_page = $this->default_ipp;
  30.         } else {
  31.             if (!is_numeric($this->items_per_page) OR $this->items_per_page <= 0)
  32.                 $this->items_per_page = $this->default_ipp;
  33.             $this->num_pages = ceil($this->items_total / $this->items_per_page);
  34.         }
  35.         $this->current_page = (int) $_GET['page']; // must be numeric > 0
  36.         if ($this->current_page < 1 Or !is_numeric($this->current_page))
  37.             $this->current_page = 1;
  38.         if ($this->current_page > $this->num_pages)
  39.             $this->current_page = $this->num_pages;
  40.         $prev_page = $this->current_page - 1;
  41.         $next_page = $this->current_page + 1;
  42.        
  43.         if ($_GET) {
  44.             $args = explode("&", $_SERVER['QUERY_STRING']);
  45.             foreach ($args as $arg) {
  46.                 $keyval = explode("=", $arg);
  47.                 if ($keyval[0] != "page" And $keyval[0] != "ipp")
  48.                     $this->querystring .= "&" . $arg;
  49.             }
  50.         }
  51.        
  52.         if ($_POST) {
  53.             foreach ($_POST as $key => $val) {
  54.                 if ($key != "page" And $key != "ipp")
  55.                     $this->querystring .= "&$key=$val";
  56.             }
  57.         }
  58.        
  59.         if ($this->num_pages > 10) {
  60.             $this->return = ($this->current_page != 1 And $this->items_total >= 10) ? "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$prev_page&ipp=$this->items_per_page$this->querystring\">&laquo; Previous</a> " : "<span class=\"inactive\" href=\"#\">&laquo; Previous</span> ";
  61.            
  62.             $this->start_range = $this->current_page - floor($this->mid_range / 2);
  63.             $this->end_range   = $this->current_page + floor($this->mid_range / 2);
  64.            
  65.             if ($this->start_range <= 0) {
  66.                 $this->end_range += abs($this->start_range) + 1;
  67.                 $this->start_range = 1;
  68.             }
  69.             if ($this->end_range > $this->num_pages) {
  70.                 $this->start_range -= $this->end_range - $this->num_pages;
  71.                 $this->end_range = $this->num_pages;
  72.             }
  73.             $this->range = range($this->start_range, $this->end_range);
  74.            
  75.             for ($i = 1; $i <= $this->num_pages; $i++) {
  76.                 if ($this->range[0] > 2 And $i == $this->range[0])
  77.                     $this->return .= " ... ";
  78.                 // loop through all pages. if first, last, or in range, display
  79.                 if ($i == 1 Or $i == $this->num_pages Or in_array($i, $this->range)) {
  80.                     $this->return .= ($i == $this->current_page And $_GET['page'] != 'All') ? "<a title=\"Go to page $i of $this->num_pages\" class=\"current\" href=\"#\">$i</a> " : "<a class=\"paginate\" title=\"Go to page $i of $this->num_pages\" href=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
  81.                 }
  82.                 if ($this->range[$this->mid_range - 1] < $this->num_pages - 1 And $i == $this->range[$this->mid_range - 1])
  83.                     $this->return .= " ... ";
  84.             }
  85.             $this->return .= (($this->current_page != $this->num_pages And $this->items_total >= 10) And ($_GET['page'] != 'All')) ? "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$next_page&ipp=$this->items_per_page$this->querystring\">Next &raquo;</a>\n" : "<span class=\"inactive\" href=\"#\">&raquo; Next</span>\n";
  86.             $this->return .= ($_GET['page'] == 'All') ? "<a class=\"current\" style=\"margin-left:10px\" href=\"#\">All</a> \n" : "<a class=\"paginate\" style=\"margin-left:10px\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
  87.         } else {
  88.             for ($i = 1; $i <= $this->num_pages; $i++) {
  89.                 $this->return .= ($i == $this->current_page) ? "<a class=\"current\" href=\"#\">$i</a> " : "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=$i&ipp=$this->items_per_page$this->querystring\">$i</a> ";
  90.             }
  91.             $this->return .= "<a class=\"paginate\" href=\"$_SERVER[PHP_SELF]?page=1&ipp=All$this->querystring\">All</a> \n";
  92.         }
  93.         $this->low   = ($this->current_page - 1) * $this->items_per_page;
  94.         $this->high  = ($_GET['ipp'] == 'All') ? $this->items_total : ($this->current_page * $this->items_per_page) - 1;
  95.         $this->limit = ($_GET['ipp'] == 'All') ? "" : " LIMIT $this->low,$this->items_per_page";
  96.     }
  97.    
  98.     function display_items_per_page()
  99.     {
  100.         $items     = '';
  101.         $ipp_array = array(
  102.             10,
  103.             25,
  104.             50,
  105.             100,
  106.             'All'
  107.         );
  108.         foreach ($ipp_array as $ipp_opt)
  109.             $items .= ($ipp_opt == $this->items_per_page) ? "<option selected value=\"$ipp_opt\">$ipp_opt</option>\n" : "<option value=\"$ipp_opt\">$ipp_opt</option>\n";
  110.         return "<span class=\"paginate\">Items per page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page=1&ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n";
  111.     }
  112.    
  113.     function display_jump_menu()
  114.     {
  115.         for ($i = 1; $i <= $this->num_pages; $i++) {
  116.             $option .= ($i == $this->current_page) ? "<option value=\"$i\" selected>$i</option>\n" : "<option value=\"$i\">$i</option>\n";
  117.         }
  118.         return "<span class=\"paginate\">Page:</span><select class=\"paginate\" onchange=\"window.location='$_SERVER[PHP_SELF]?page='+this[this.selectedIndex].value+'&ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n";
  119.     }
  120.    
  121.     function display_pages()
  122.     {
  123.         return $this->return;
  124.     }
  125. }
  126. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement