daily pastebin goal
75%
SHARE
TWEET

Untitled

a guest Jun 19th, 2018 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2.  
  3. include_once(BASEPATH.'libraries/Pagination.php');
  4.  
  5. class PaginationSortable extends CI_Pagination {
  6.     /**
  7.      * Generate the pagination links
  8.      *
  9.      * @access  public
  10.      * @return  string
  11.      */
  12.     function create_links($extrauri=null)
  13.     {
  14.         if (isset($extrauri)){
  15.             $extrauri = "/" . $extrauri;
  16.         } else {
  17.             $extrauri = "";
  18.         }
  19.        
  20.         // If our item count or per-page total is zero there is no need to continue.
  21.         if ($this->total_rows == 0 OR $this->per_page == 0)
  22.         {
  23.             return '';
  24.         }
  25.  
  26.         // Calculate the total number of pages
  27.         $num_pages = ceil($this->total_rows / $this->per_page);
  28.  
  29.         // Is there only one page? Hm... nothing more to do here then.
  30.         if ($num_pages == 1)
  31.         {
  32.             return '';
  33.         }
  34.  
  35.         // Determine the current page number.
  36.         $CI =& get_instance();
  37.  
  38.         if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  39.         {
  40.             if ($CI->input->get($this->query_string_segment) != 0)
  41.             {
  42.                 $this->cur_page = $CI->input->get($this->query_string_segment);
  43.  
  44.                 // Prep the current page - no funny business!
  45.                 $this->cur_page = (int) $this->cur_page;
  46.             }
  47.         }
  48.         else
  49.         {
  50.             if ($CI->uri->segment($this->uri_segment) != 0)
  51.             {
  52.                 $this->cur_page = $CI->uri->segment($this->uri_segment);
  53.  
  54.                 // Prep the current page - no funny business!
  55.                 $this->cur_page = (int) $this->cur_page;
  56.             }
  57.         }
  58.  
  59.         $this->num_links = (int)$this->num_links;
  60.  
  61.         if ($this->num_links < 1)
  62.         {
  63.             show_error('Your number of links must be a positive number.');
  64.         }
  65.  
  66.         if ( ! is_numeric($this->cur_page))
  67.         {
  68.             $this->cur_page = 0;
  69.         }
  70.  
  71.         // Is the page number beyond the result range?
  72.         // If so we show the last page
  73.         if ($this->cur_page > $this->total_rows)
  74.         {
  75.             $this->cur_page = ($num_pages - 1) * $this->per_page;
  76.         }
  77.  
  78.         $uri_page_number = $this->cur_page;
  79.         $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  80.  
  81.         // Calculate the start and end numbers. These determine
  82.         // which number to start and end the digit links with
  83.         $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  84.         $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  85.  
  86.         // Is pagination being used over GET or POST?  If get, add a per_page query
  87.         // string. If post, add a trailing slash to the base URL if needed
  88.         if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  89.         {
  90.             $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
  91.         }
  92.         else
  93.         {
  94.             $this->base_url = rtrim($this->base_url, '/') .'/';
  95.         }
  96.  
  97.         // And here we go...
  98.         $output = '';
  99.  
  100.         // Render the "First" link
  101.         if  ($this->cur_page > ($this->num_links + 1))
  102.         {
  103.             $output .= $this->first_tag_open.'<a href="'.$this->base_url."0".$extrauri.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  104.         }
  105.  
  106.         // Render the "previous" link
  107.         if  ($this->cur_page != 1)
  108.         {
  109.             $i = $uri_page_number - $this->per_page;
  110.             $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.$extrauri.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  111.         }
  112.  
  113.         // Write the digit links
  114.         for ($loop = $start -1; $loop <= $end; $loop++)
  115.         {
  116.             $i = ($loop * $this->per_page) - $this->per_page;
  117.  
  118.             if ($i >= 0)
  119.             {
  120.                 if ($this->cur_page == $loop)
  121.                 {
  122.                     $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  123.                 }
  124.                 else
  125.                 {
  126.                     $output .= $this->num_tag_open.'<a href="'.$this->base_url.$i.$extrauri.'">'.$loop.'</a>'.$this->num_tag_close;
  127.                 }
  128.             }
  129.         }
  130.  
  131.         // Render the "next" link
  132.         if ($this->cur_page < $num_pages)
  133.         {
  134.             $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).$extrauri.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  135.         }
  136.  
  137.         // Render the "Last" link
  138.         if (($this->cur_page + $this->num_links) < $num_pages)
  139.         {
  140.             $i = (($num_pages * $this->per_page) - $this->per_page);
  141.             $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.$extrauri.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  142.         }
  143.  
  144.         // Kill double slashes.  Note: Sometimes we can end up with a double slash
  145.         // in the penultimate link so we'll kill all double slashes.
  146.         $output = preg_replace("#([^:])//+#", "\\1/", $output);
  147.  
  148.         // Add the wrapper HTML if exists
  149.         $output = $this->full_tag_open.$output.$this->full_tag_close;
  150.  
  151.         return $output;
  152.     }
  153. }
  154.  
  155. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top