Pagination.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2006, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Pagination Class
00020  *
00021  * @package             CodeIgniter
00022  * @subpackage  Libraries
00023  * @category    Pagination
00024  * @author              ExpressionEngine Dev Team
00025  * @link                http://codeigniter.com/user_guide/libraries/pagination.html
00026  */
00027 class CI_Pagination {
00028 
00029         var $base_url                   = ''; // The page we are linking to
00030         var $total_rows                 = ''; // Total number of items (database results)
00031         var $per_page                   = 10; // Max number of items you want shown per page
00032         var $num_links                  =  2; // Number of "digit" links to show before/after the currently viewed page
00033         var $cur_page                   =  0; // The current page being viewed
00034         var $first_link                 = '&lsaquo; First';
00035         var $next_link                  = '&gt;';
00036         var $prev_link                  = '&lt;';
00037         var $last_link                  = 'Last &rsaquo;';
00038         var $uri_segment                = 3;
00039         var $full_tag_open              = '';
00040         var $full_tag_close             = '';
00041         var $first_tag_open             = '';
00042         var $first_tag_close    = '&nbsp;';
00043         var $last_tag_open              = '&nbsp;';
00044         var $last_tag_close             = '';
00045         var $cur_tag_open               = '&nbsp;<b>';
00046         var $cur_tag_close              = '</b>';
00047         var $next_tag_open              = '&nbsp;';
00048         var $next_tag_close             = '&nbsp;';
00049         var $prev_tag_open              = '&nbsp;';
00050         var $prev_tag_close             = '';
00051         var $num_tag_open               = '&nbsp;';
00052         var $num_tag_close              = '';
00053         var $page_query_string  = FALSE;
00054         var $query_string_segment = 'per_page';
00055 
00056         /**
00057          * Constructor
00058          *
00059          * @access      public
00060          * @param       array   initialization parameters
00061          */
00062         function CI_Pagination($params = array())
00063         {
00064                 if (count($params) > 0)
00065                 {
00066                         $this->initialize($params);             
00067                 }
00068                 
00069                 log_message('debug', "Pagination Class Initialized");
00070         }
00071         
00072         // --------------------------------------------------------------------
00073         
00074         /**
00075          * Initialize Preferences
00076          *
00077          * @access      public
00078          * @param       array   initialization parameters
00079          * @return      void
00080          */
00081         function initialize($params = array())
00082         {
00083                 if (count($params) > 0)
00084                 {
00085                         foreach ($params as $key => $val)
00086                         {
00087                                 if (isset($this->$key))
00088                                 {
00089                                         $this->$key = $val;
00090                                 }
00091                         }               
00092                 }
00093         }
00094         
00095         // --------------------------------------------------------------------
00096         
00097         /**
00098          * Generate the pagination links
00099          *
00100          * @access      public
00101          * @return      string
00102          */     
00103         function create_links()
00104         {
00105                 // If our item count or per-page total is zero there is no need to continue.
00106                 if ($this->total_rows == 0 OR $this->per_page == 0)
00107                 {
00108                    return '';
00109                 }
00110 
00111                 // Calculate the total number of pages
00112                 $num_pages = ceil($this->total_rows / $this->per_page);
00113 
00114                 // Is there only one page? Hm... nothing more to do here then.
00115                 if ($num_pages == 1)
00116                 {
00117                         return '';
00118                 }
00119 
00120                 // Determine the current page number.           
00121                 $CI =& get_instance();  
00122                 
00123                 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
00124                 {
00125                         if ($CI->input->get($this->query_string_segment) != 0)
00126                         {
00127                                 $this->cur_page = $CI->input->get($this->query_string_segment);
00128                                 
00129                                 // Prep the current page - no funny business!
00130                                 $this->cur_page = (int) $this->cur_page;
00131                         }
00132                 }
00133                 else
00134                 {
00135                         if ($CI->uri->segment($this->uri_segment) != 0)
00136                         {
00137                                 $this->cur_page = $CI->uri->segment($this->uri_segment);
00138                                 
00139                                 // Prep the current page - no funny business!
00140                                 $this->cur_page = (int) $this->cur_page;
00141                         }
00142                 }
00143 
00144                 $this->num_links = (int)$this->num_links;
00145                 
00146                 if ($this->num_links < 1)
00147                 {
00148                         show_error('Your number of links must be a positive number.');
00149                 }
00150                                 
00151                 if ( ! is_numeric($this->cur_page))
00152                 {
00153                         $this->cur_page = 0;
00154                 }
00155                 
00156                 // Is the page number beyond the result range?
00157                 // If so we show the last page
00158                 if ($this->cur_page > $this->total_rows)
00159                 {
00160                         $this->cur_page = ($num_pages - 1) * $this->per_page;
00161                 }
00162                 
00163                 $uri_page_number = $this->cur_page;
00164                 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
00165 
00166                 // Calculate the start and end numbers. These determine
00167                 // which number to start and end the digit links with
00168                 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
00169                 $end   = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
00170 
00171                 // Is pagination being used over GET or POST?  If get, add a per_page query
00172                 // string. If post, add a trailing slash to the base URL if needed
00173                 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
00174                 {
00175                         $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
00176                 }
00177                 else
00178                 {
00179                         $this->base_url = rtrim($this->base_url, '/') .'/';
00180                 }
00181 
00182                 // And here we go...
00183                 $output = '';
00184 
00185                 // Render the "First" link
00186                 if  ($this->cur_page > $this->num_links)
00187                 {
00188                         $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
00189                 }
00190 
00191                 // Render the "previous" link
00192                 if  ($this->cur_page != 1)
00193                 {
00194                         $i = $uri_page_number - $this->per_page;
00195                         if ($i == 0) $i = '';
00196                         $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
00197                 }
00198 
00199                 // Write the digit links
00200                 for ($loop = $start -1; $loop <= $end; $loop++)
00201                 {
00202                         $i = ($loop * $this->per_page) - $this->per_page;
00203                                         
00204                         if ($i >= 0)
00205                         {
00206                                 if ($this->cur_page == $loop)
00207                                 {
00208                                         $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
00209                                 }
00210                                 else
00211                                 {
00212                                         $n = ($i == 0) ? '' : $i;
00213                                         $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
00214                                 }
00215                         }
00216                 }
00217 
00218                 // Render the "next" link
00219                 if ($this->cur_page < $num_pages)
00220                 {
00221                         $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
00222                 }
00223 
00224                 // Render the "Last" link
00225                 if (($this->cur_page + $this->num_links) < $num_pages)
00226                 {
00227                         $i = (($num_pages * $this->per_page) - $this->per_page);
00228                         $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
00229                 }
00230 
00231                 // Kill double slashes.  Note: Sometimes we can end up with a double slash
00232                 // in the penultimate link so we'll kill all double slashes.
00233                 $output = preg_replace("#([^:])//+#", "\\1/", $output);
00234 
00235                 // Add the wrapper HTML if exists
00236                 $output = $this->full_tag_open.$output.$this->full_tag_close;
00237                 
00238                 return $output;         
00239         }
00240 }
00241 // END Pagination Class
00242 
00243 /* End of file Pagination.php */
00244 /* Location: ./system/libraries/Pagination.php */