blob: 867d214fcbc2aefd942405d136c9a9ba1f2e7b4f [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 */
admine79dc712006-09-26 03:52:45 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Pagination Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Pagination
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/libraries/pagination.html
26 */
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 = intval($this->total_rows / $this->per_page);
111
112 // Use modulus to see if our division has a remainder.If so, add one to our page number.
113 if ($this->total_rows % $this->per_page)
114 {
115 $num_pages++;
116 }
117
118 // Is there only one page? Hm... nothing more to do here then.
119 if ($num_pages == 1)
120 {
121 return '';
122 }
123
124 // Determine the current page number.
125 $obj =& get_instance();
126 if ($obj->uri->segment($this->uri_segment) != 0)
127 {
128 $this->cur_page = $obj->uri->segment($this->uri_segment);
129 }
130
admin1cf89aa2006-09-03 18:24:39 +0000131 if ( ! is_numeric($this->cur_page))
adminb0dd10f2006-08-25 17:25:49 +0000132 {
133 $this->cur_page = 0;
134 }
135
136 $uri_page_number = $this->cur_page;
137 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
138
139 // Calculate the start and end numbers. These determine
140 // which number to start and end the digit links with
141 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
142 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
143
144 // Add a trailing slash to the base URL if needed
145 $this->base_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->base_url);
146
147 // And here we go...
148 $output = '';
149
150 // Render the "First" link
151 if ($this->cur_page > $this->num_links)
152 {
153 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
154 }
155
156 // Render the "previous" link
157 if (($this->cur_page - $this->num_links) >= 0)
158 {
159 $i = $uri_page_number - $this->per_page;
160 if ($i == 0) $i = '';
161 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
162 }
163
164 // Write the digit links
165 for ($loop = $start -1; $loop <= $end; $loop++)
166 {
167 $i = ($loop * $this->per_page) - $this->per_page;
168
169 if ($i >= 0)
170 {
171 if ($this->cur_page == $loop)
172 {
173 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
174 }
175 else
176 {
177 $n = ($i == 0) ? '' : $i;
178 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
179 }
180 }
181 }
182
183 // Render the "next" link
184 if ($this->cur_page < $num_pages)
185 {
186 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
187 }
188
189 // Render the "Last" link
190 if (($this->cur_page + $this->num_links) < $num_pages)
191 {
192 $i = (($num_pages * $this->per_page) - $this->per_page);
193 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
194 }
195
196 // Kill double slashes. Note: Sometimes we can end up with a double slash
197 // in the penultimate link so we'll kill all double shashes.
198 $output = preg_replace("#([^:])//+#", "\\1/", $output);
199
200 // Add the wrapper HTML if exists
201 $output = $this->full_tag_open.$output.$this->full_tag_close;
202
203 return $output;
204 }
205}
206// END Pagination Class
207?>