blob: 28b74f1f01f6589448095da381c4be1e24efb007 [file] [log] [blame]
Derek Allardd2df9bc2007-04-15 17:41:17 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 = '';
53
54 /**
55 * Constructor
56 *
57 * @access public
58 * @param array initialization parameters
59 */
60 function CI_Pagination($params = array())
61 {
62 if (count($params) > 0)
63 {
64 $this->initialize($params);
65 }
66
67 log_message('debug', "Pagination Class Initialized");
68 }
69
70 // --------------------------------------------------------------------
71
72 /**
73 * Initialize Preferences
74 *
75 * @access public
76 * @param array initialization parameters
77 * @return void
78 */
79 function initialize($params = array())
80 {
81 if (count($params) > 0)
82 {
83 foreach ($params as $key => $val)
84 {
85 if (isset($this->$key))
86 {
87 $this->$key = $val;
88 }
89 }
90 }
91 }
92
93 // --------------------------------------------------------------------
94
95 /**
96 * Generate the pagination links
97 *
98 * @access public
99 * @return string
100 */
101 function create_links()
102 {
103 // If our item count or per-page total is zero there is no need to continue.
104 if ($this->total_rows == 0 OR $this->per_page == 0)
105 {
106 return '';
107 }
108
109 // Calculate the total number of pages
110 $num_pages = ceil($this->total_rows / $this->per_page);
111
112 // Is there only one page? Hm... nothing more to do here then.
113 if ($num_pages == 1)
114 {
115 return '';
116 }
117
118 // Determine the current page number.
119 $CI =& get_instance();
120 if ($CI->uri->segment($this->uri_segment) != 0)
121 {
122 $this->cur_page = $CI->uri->segment($this->uri_segment);
123
124 // Prep the current page - no funny business!
Derek Allard31d363b2007-05-02 01:18:58 +0000125 $this->cur_page = (int) $this->cur_page;
Derek Allardd2df9bc2007-04-15 17:41:17 +0000126 }
Derek Allard825bc9b2008-01-16 23:37:42 +0000127
128 $this->num_links = (int)$this->num_links;
129
130 if ($this->num_links < 1)
131 {
132 show_error('Your number of links must be a positive number.');
133 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000134
135 if ( ! is_numeric($this->cur_page))
136 {
137 $this->cur_page = 0;
138 }
139
140 // Is the page number beyond the result range?
141 // If so we show the last page
142 if ($this->cur_page > $this->total_rows)
143 {
144 $this->cur_page = ($num_pages - 1) * $this->per_page;
145 }
146
147 $uri_page_number = $this->cur_page;
148 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
149
150 // Calculate the start and end numbers. These determine
151 // which number to start and end the digit links with
152 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
153 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
154
155 // Add a trailing slash to the base URL if needed
Derek Allardbbc5ec22007-05-02 11:34:08 +0000156 $this->base_url = rtrim($this->base_url, '/') .'/';
157
Derek Allardd2df9bc2007-04-15 17:41:17 +0000158 // And here we go...
159 $output = '';
160
161 // Render the "First" link
162 if ($this->cur_page > $this->num_links)
163 {
164 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
165 }
166
167 // Render the "previous" link
Derek Allardf37fa6e2008-01-30 01:15:03 +0000168 if ($this->cur_page != 1)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000169 {
170 $i = $uri_page_number - $this->per_page;
171 if ($i == 0) $i = '';
172 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
173 }
174
175 // Write the digit links
176 for ($loop = $start -1; $loop <= $end; $loop++)
177 {
178 $i = ($loop * $this->per_page) - $this->per_page;
179
180 if ($i >= 0)
181 {
182 if ($this->cur_page == $loop)
183 {
184 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
185 }
186 else
187 {
188 $n = ($i == 0) ? '' : $i;
189 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
190 }
191 }
192 }
193
194 // Render the "next" link
195 if ($this->cur_page < $num_pages)
196 {
197 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
198 }
199
200 // Render the "Last" link
201 if (($this->cur_page + $this->num_links) < $num_pages)
202 {
203 $i = (($num_pages * $this->per_page) - $this->per_page);
204 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
205 }
206
207 // Kill double slashes. Note: Sometimes we can end up with a double slash
208 // in the penultimate link so we'll kill all double slashes.
209 $output = preg_replace("#([^:])//+#", "\\1/", $output);
210
211 // Add the wrapper HTML if exists
212 $output = $this->full_tag_open.$output.$this->full_tag_close;
213
214 return $output;
215 }
216}
217// END Pagination Class
adminb0dd10f2006-08-25 17:25:49 +0000218?>