blob: cd55d56e0f6bfaed297e7bcee4935b52d1554725 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
admine79dc712006-09-26 03:52:45 +000016// INITIALIZE THE CLASS ---------------------------------------------------
17
18$config = array();
19if (file_exists(APPPATH.'config/pagination'.EXT))
20{
21 include_once(APPPATH.'config/pagination'.EXT);
22}
23
24$obj =& get_instance();
admin7981a9a2006-09-26 07:52:09 +000025$obj->init_class('CI_Pagination', '', $config);
admine79dc712006-09-26 03:52:45 +000026
adminb0dd10f2006-08-25 17:25:49 +000027// ------------------------------------------------------------------------
28
29/**
30 * Pagination Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Pagination
35 * @author Rick Ellis
36 * @link http://www.codeigniter.com/user_guide/libraries/pagination.html
37 */
38class CI_Pagination {
39
40 var $base_url = ''; // The page we are linking to
41 var $total_rows = ''; // Total number of items (database results)
42 var $per_page = 10; // Max number of items you want shown per page
43 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
44 var $cur_page = 0; // The current page being viewed
45 var $first_link = '&lsaquo; First';
46 var $next_link = '&gt;';
47 var $prev_link = '&lt;';
48 var $last_link = 'Last &rsaquo;';
49 var $uri_segment = 3;
50 var $full_tag_open = '';
51 var $full_tag_close = '';
52 var $first_tag_open = '';
53 var $first_tag_close = '&nbsp;';
54 var $last_tag_open = '&nbsp;';
55 var $last_tag_close = '';
56 var $cur_tag_open = '&nbsp;<b>';
57 var $cur_tag_close = '</b>';
58 var $next_tag_open = '&nbsp;';
59 var $next_tag_close = '&nbsp;';
60 var $prev_tag_open = '&nbsp;';
61 var $prev_tag_close = '';
62 var $num_tag_open = '&nbsp;';
63 var $num_tag_close = '';
64
65 /**
66 * Constructor
67 *
68 * @access public
69 * @param array initialization parameters
70 */
71 function CI_Pagination($params = array())
72 {
73 if (count($params) > 0)
74 {
75 $this->initialize($params);
76 }
77
78 log_message('debug', "Pagination Class Initialized");
79 }
80
81 // --------------------------------------------------------------------
82
83 /**
84 * Initialize Preferences
85 *
86 * @access public
87 * @param array initialization parameters
88 * @return void
89 */
90 function initialize($params = array())
91 {
92 if (count($params) > 0)
93 {
94 foreach ($params as $key => $val)
95 {
96 if (isset($this->$key))
97 {
98 $this->$key = $val;
99 }
100 }
101 }
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Generate the pagination links
108 *
109 * @access public
110 * @return string
111 */
112 function create_links()
113 {
114 // If our item count or per-page total is zero there is no need to continue.
115 if ($this->total_rows == 0 OR $this->per_page == 0)
116 {
117 return '';
118 }
119
120 // Calculate the total number of pages
121 $num_pages = intval($this->total_rows / $this->per_page);
122
123 // Use modulus to see if our division has a remainder.If so, add one to our page number.
124 if ($this->total_rows % $this->per_page)
125 {
126 $num_pages++;
127 }
128
129 // Is there only one page? Hm... nothing more to do here then.
130 if ($num_pages == 1)
131 {
132 return '';
133 }
134
135 // Determine the current page number.
136 $obj =& get_instance();
137 if ($obj->uri->segment($this->uri_segment) != 0)
138 {
139 $this->cur_page = $obj->uri->segment($this->uri_segment);
140 }
141
admin1cf89aa2006-09-03 18:24:39 +0000142 if ( ! is_numeric($this->cur_page))
adminb0dd10f2006-08-25 17:25:49 +0000143 {
144 $this->cur_page = 0;
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
156 $this->base_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->base_url);
157
158 // 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
168 if (($this->cur_page - $this->num_links) >= 0)
169 {
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 shashes.
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
218?>