blob: 2b8d3e45428e47d704d0827c4677bc28388ea87a [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Pagination Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Pagination
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/libraries/pagination.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27class CI_Pagination {
28
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 = '&lsaquo; First';
35 var $next_link = '&gt;';
36 var $prev_link = '&lt;';
37 var $last_link = 'Last &rsaquo;';
38 var $uri_segment = 3;
39 var $full_tag_open = '';
40 var $full_tag_close = '';
41 var $first_tag_open = '';
42 var $first_tag_close = '&nbsp;';
43 var $last_tag_open = '&nbsp;';
44 var $last_tag_close = '';
45 var $cur_tag_open = '&nbsp;<b>';
46 var $cur_tag_close = '</b>';
47 var $next_tag_open = '&nbsp;';
48 var $next_tag_close = '&nbsp;';
49 var $prev_tag_open = '&nbsp;';
50 var $prev_tag_close = '';
51 var $num_tag_open = '&nbsp;';
52 var $num_tag_close = '';
Derek Allard8ddc0db2008-05-21 04:54:39 +000053 var $page_query_string = FALSE;
Derek Allardd2df9bc2007-04-15 17:41:17 +000054
55 /**
56 * Constructor
57 *
58 * @access public
59 * @param array initialization parameters
60 */
61 function CI_Pagination($params = array())
62 {
63 if (count($params) > 0)
64 {
65 $this->initialize($params);
66 }
67
68 log_message('debug', "Pagination Class Initialized");
69 }
70
71 // --------------------------------------------------------------------
72
73 /**
74 * Initialize Preferences
75 *
76 * @access public
77 * @param array initialization parameters
78 * @return void
79 */
80 function initialize($params = array())
81 {
82 if (count($params) > 0)
83 {
84 foreach ($params as $key => $val)
85 {
86 if (isset($this->$key))
87 {
88 $this->$key = $val;
89 }
90 }
91 }
92 }
93
94 // --------------------------------------------------------------------
95
96 /**
97 * Generate the pagination links
98 *
99 * @access public
100 * @return string
101 */
102 function create_links()
103 {
104 // If our item count or per-page total is zero there is no need to continue.
105 if ($this->total_rows == 0 OR $this->per_page == 0)
106 {
107 return '';
108 }
109
110 // Calculate the total number of pages
111 $num_pages = ceil($this->total_rows / $this->per_page);
112
113 // Is there only one page? Hm... nothing more to do here then.
114 if ($num_pages == 1)
115 {
116 return '';
117 }
118
119 // Determine the current page number.
120 $CI =& get_instance();
121 if ($CI->uri->segment($this->uri_segment) != 0)
122 {
123 $this->cur_page = $CI->uri->segment($this->uri_segment);
124
125 // Prep the current page - no funny business!
Derek Allard31d363b2007-05-02 01:18:58 +0000126 $this->cur_page = (int) $this->cur_page;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000127 }
Derek Allard825bc9b2008-01-16 23:37:42 +0000128
129 $this->num_links = (int)$this->num_links;
130
131 if ($this->num_links < 1)
132 {
133 show_error('Your number of links must be a positive number.');
134 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000135
Derek Jones0b59f272008-05-13 04:22:33 +0000136 if ( ! is_numeric($this->cur_page))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000137 {
138 $this->cur_page = 0;
139 }
140
141 // Is the page number beyond the result range?
142 // If so we show the last page
143 if ($this->cur_page > $this->total_rows)
144 {
145 $this->cur_page = ($num_pages - 1) * $this->per_page;
146 }
147
148 $uri_page_number = $this->cur_page;
149 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
150
151 // Calculate the start and end numbers. These determine
152 // which number to start and end the digit links with
153 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
154 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
155
Derek Allard8ddc0db2008-05-21 04:54:39 +0000156 // Is pagination being used over GET or POST? If get, add a per_page query
157 // string. If post, add a trailing slash to the base URL if needed
158 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
159 {
160 $this->base_url = rtrim($this->base_url).AMP.'per_page=';
161 }
162 else
163 {
164 $this->base_url = rtrim($this->base_url, '/') .'/';
165 }
Derek Allardbbc5ec22007-05-02 11:34:08 +0000166
Derek Allardd2df9bc2007-04-15 17:41:17 +0000167 // And here we go...
168 $output = '';
169
170 // Render the "First" link
171 if ($this->cur_page > $this->num_links)
172 {
173 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
174 }
175
176 // Render the "previous" link
Derek Allardf37fa6e2008-01-30 01:15:03 +0000177 if ($this->cur_page != 1)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000178 {
179 $i = $uri_page_number - $this->per_page;
180 if ($i == 0) $i = '';
181 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
182 }
183
184 // Write the digit links
185 for ($loop = $start -1; $loop <= $end; $loop++)
186 {
187 $i = ($loop * $this->per_page) - $this->per_page;
188
189 if ($i >= 0)
190 {
191 if ($this->cur_page == $loop)
192 {
193 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
194 }
195 else
196 {
197 $n = ($i == 0) ? '' : $i;
198 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
199 }
200 }
201 }
202
203 // Render the "next" link
204 if ($this->cur_page < $num_pages)
205 {
206 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
207 }
208
209 // Render the "Last" link
210 if (($this->cur_page + $this->num_links) < $num_pages)
211 {
212 $i = (($num_pages * $this->per_page) - $this->per_page);
213 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
214 }
215
216 // Kill double slashes. Note: Sometimes we can end up with a double slash
217 // in the penultimate link so we'll kill all double slashes.
218 $output = preg_replace("#([^:])//+#", "\\1/", $output);
219
220 // Add the wrapper HTML if exists
221 $output = $this->full_tag_open.$output.$this->full_tag_close;
222
223 return $output;
224 }
225}
226// END Pagination Class
Derek Jones0b59f272008-05-13 04:22:33 +0000227
228/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000229/* Location: ./system/libraries/Pagination.php */