1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, 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 $prefix = ''; // A custom prefix added to the path.
31 var $suffix = ''; // A custom suffix added to the path.
33 var $total_rows = ''; // Total number of items (database results)
34 var $per_page = 10; // Max number of items you want shown per page
35 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
36 var $cur_page = 0; // The current page being viewed
37 var $first_link = '‹ First';
38 var $next_link = '>';
39 var $prev_link = '<';
40 var $last_link = 'Last ›';
42 var $full_tag_open = '';
43 var $full_tag_close = '';
44 var $first_tag_open = '';
45 var $first_tag_close = ' ';
46 var $last_tag_open = ' ';
47 var $last_tag_close = '';
48 var $first_url = ''; // Alternative URL for the First Page.
49 var $cur_tag_open = ' <strong>';
50 var $cur_tag_close = '</strong>';
51 var $next_tag_open = ' ';
52 var $next_tag_close = ' ';
53 var $prev_tag_open = ' ';
54 var $prev_tag_close = '';
55 var $num_tag_open = ' ';
56 var $num_tag_close = '';
57 var $page_query_string = FALSE;
58 var $query_string_segment = 'per_page';
59 var $display_pages = TRUE;
60 var $anchor_class = '';
66 * @param array initialization parameters
68 public function __construct($params = array())
70 if (count($params) > 0)
72 $this->initialize($params);
75 if ($this->anchor_class != '')
77 $this->anchor_class = 'class="'.$this->anchor_class.'" ';
80 log_message('debug', "Pagination Class Initialized");
83 // --------------------------------------------------------------------
86 * Initialize Preferences
89 * @param array initialization parameters
92 function initialize($params = array())
94 if (count($params) > 0)
96 foreach ($params as $key => $val)
98 if (isset($this->$key))
106 // --------------------------------------------------------------------
109 * Generate the pagination links
114 function create_links()
116 // If our item count or per-page total is zero there is no need to continue.
117 if ($this->total_rows == 0 OR $this->per_page == 0)
122 // Calculate the total number of pages
123 $num_pages = ceil($this->total_rows / $this->per_page);
125 // Is there only one page? Hm... nothing more to do here then.
131 // Determine the current page number.
132 $CI =& get_instance();
134 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
136 if ($CI->input->get($this->query_string_segment) != 0)
138 $this->cur_page = $CI->input->get($this->query_string_segment);
140 // Prep the current page - no funny business!
141 $this->cur_page = (int) $this->cur_page;
146 if ($CI->uri->segment($this->uri_segment) != 0)
148 $this->cur_page = $CI->uri->segment($this->uri_segment);
150 // Prep the current page - no funny business!
151 $this->cur_page = (int) $this->cur_page;
155 $this->num_links = (int)$this->num_links;
157 if ($this->num_links < 1)
159 show_error('Your number of links must be a positive number.');
162 if ( ! is_numeric($this->cur_page))
167 // Is the page number beyond the result range?
168 // If so we show the last page
169 if ($this->cur_page > $this->total_rows)
171 $this->cur_page = ($num_pages - 1) * $this->per_page;
174 $uri_page_number = $this->cur_page;
175 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
177 // Calculate the start and end numbers. These determine
178 // which number to start and end the digit links with
179 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
180 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
182 // Is pagination being used over GET or POST? If get, add a per_page query
183 // string. If post, add a trailing slash to the base URL if needed
184 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
186 $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
190 $this->base_url = rtrim($this->base_url, '/') .'/';
196 // Render the "First" link
197 if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
199 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
200 $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
203 // Render the "previous" link
204 if ($this->prev_link !== FALSE AND $this->cur_page != 1)
206 $i = $uri_page_number - $this->per_page;
208 if ($i == 0 && $this->first_url != '')
210 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
214 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
215 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
221 if ($this->display_pages !== FALSE)
223 // Write the digit links
224 for ($loop = $start -1; $loop <= $end; $loop++)
226 $i = ($loop * $this->per_page) - $this->per_page;
230 if ($this->cur_page == $loop)
232 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
236 $n = ($i == 0) ? '' : $i;
238 if ($n == '' && $this->first_url != '')
240 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
244 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
246 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
253 // Render the "next" link
254 if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
256 $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
259 // Render the "Last" link
260 if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
262 $i = (($num_pages * $this->per_page) - $this->per_page);
263 $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
266 // Kill double slashes. Note: Sometimes we can end up with a double slash
267 // in the penultimate link so we'll kill all double slashes.
268 $output = preg_replace("#([^:])//+#", "\\1/", $output);
270 // Add the wrapper HTML if exists
271 $output = $this->full_tag_open.$output.$this->full_tag_close;
276 // END Pagination Class
278 /* End of file Pagination.php */
279 /* Location: ./system/libraries/Pagination.php */