1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Pagination
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/pagination.html
29 var $base_url = ''; // The page we are linking to
30 var $total_rows = ''; // Total number of items (database results)
31 var $per_page = 10; // Max number of items you want shown per page
32 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
33 var $cur_page = 0; // The current page being viewed
34 var $first_link = '‹ First';
35 var $next_link = '>';
36 var $prev_link = '<';
37 var $last_link = 'Last ›';
39 var $full_tag_open = '';
40 var $full_tag_close = '';
41 var $first_tag_open = '';
42 var $first_tag_close = ' ';
43 var $last_tag_open = ' ';
44 var $last_tag_close = '';
45 var $cur_tag_open = ' <strong>';
46 var $cur_tag_close = '</strong>';
47 var $next_tag_open = ' ';
48 var $next_tag_close = ' ';
49 var $prev_tag_open = ' ';
50 var $prev_tag_close = '';
51 var $num_tag_open = ' ';
52 var $num_tag_close = '';
53 var $page_query_string = FALSE;
54 var $query_string_segment = 'per_page';
60 * @param array initialization parameters
62 function CI_Pagination($params = array())
64 if (count($params) > 0)
66 $this->initialize($params);
69 log_message('debug', "Pagination Class Initialized");
72 // --------------------------------------------------------------------
75 * Initialize Preferences
78 * @param array initialization parameters
81 function initialize($params = array())
83 if (count($params) > 0)
85 foreach ($params as $key => $val)
87 if (isset($this->$key))
95 // --------------------------------------------------------------------
98 * Generate the pagination links
103 function create_links()
105 // If our item count or per-page total is zero there is no need to continue.
106 if ($this->total_rows == 0 OR $this->per_page == 0)
111 // Calculate the total number of pages
112 $num_pages = ceil($this->total_rows / $this->per_page);
114 // Is there only one page? Hm... nothing more to do here then.
120 // Determine the current page number.
121 $CI =& get_instance();
123 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
125 if ($CI->input->get($this->query_string_segment) != 0)
127 $this->cur_page = $CI->input->get($this->query_string_segment);
129 // Prep the current page - no funny business!
130 $this->cur_page = (int) $this->cur_page;
135 if ($CI->uri->segment($this->uri_segment) != 0)
137 $this->cur_page = $CI->uri->segment($this->uri_segment);
139 // Prep the current page - no funny business!
140 $this->cur_page = (int) $this->cur_page;
144 $this->num_links = (int)$this->num_links;
146 if ($this->num_links < 1)
148 show_error('Your number of links must be a positive number.');
151 if ( ! is_numeric($this->cur_page))
156 // Is the page number beyond the result range?
157 // If so we show the last page
158 if ($this->cur_page > $this->total_rows)
160 $this->cur_page = ($num_pages - 1) * $this->per_page;
163 $uri_page_number = $this->cur_page;
164 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
166 // Calculate the start and end numbers. These determine
167 // which number to start and end the digit links with
168 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
169 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
171 // Is pagination being used over GET or POST? If get, add a per_page query
172 // string. If post, add a trailing slash to the base URL if needed
173 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
175 $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
179 $this->base_url = rtrim($this->base_url, '/') .'/';
185 // Render the "First" link
186 if ($this->cur_page > ($this->num_links + 1))
188 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
191 // Render the "previous" link
192 if ($this->cur_page != 1)
194 $i = $uri_page_number - $this->per_page;
195 if ($i == 0) $i = '';
196 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
199 // Write the digit links
200 for ($loop = $start -1; $loop <= $end; $loop++)
202 $i = ($loop * $this->per_page) - $this->per_page;
206 if ($this->cur_page == $loop)
208 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
212 $n = ($i == 0) ? '' : $i;
213 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
218 // Render the "next" link
219 if ($this->cur_page < $num_pages)
221 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
224 // Render the "Last" link
225 if (($this->cur_page + $this->num_links) < $num_pages)
227 $i = (($num_pages * $this->per_page) - $this->per_page);
228 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
231 // Kill double slashes. Note: Sometimes we can end up with a double slash
232 // in the penultimate link so we'll kill all double slashes.
233 $output = preg_replace("#([^:])//+#", "\\1/", $output);
235 // Add the wrapper HTML if exists
236 $output = $this->full_tag_open.$output.$this->full_tag_close;
241 // END Pagination Class
243 /* End of file Pagination.php */
244 /* Location: ./system/libraries/Pagination.php */