blob: 8ea0dab19a0be477afc2e9f820a3a8439e0eb886 [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
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, EllisLab, 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
16// ------------------------------------------------------------------------
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 = 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 }
127
128 if ( ! is_numeric($this->cur_page))
129 {
130 $this->cur_page = 0;
131 }
132
133 // Is the page number beyond the result range?
134 // If so we show the last page
135 if ($this->cur_page > $this->total_rows)
136 {
137 $this->cur_page = ($num_pages - 1) * $this->per_page;
138 }
139
140 $uri_page_number = $this->cur_page;
141 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
142
143 // Calculate the start and end numbers. These determine
144 // which number to start and end the digit links with
145 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
146 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
147
148 // Add a trailing slash to the base URL if needed
149 $this->base_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->base_url);
150
151 // And here we go...
152 $output = '';
153
154 // Render the "First" link
155 if ($this->cur_page > $this->num_links)
156 {
157 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
158 }
159
160 // Render the "previous" link
161 if (($this->cur_page - $this->num_links) >= 0)
162 {
163 $i = $uri_page_number - $this->per_page;
164 if ($i == 0) $i = '';
165 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
166 }
167
168 // Write the digit links
169 for ($loop = $start -1; $loop <= $end; $loop++)
170 {
171 $i = ($loop * $this->per_page) - $this->per_page;
172
173 if ($i >= 0)
174 {
175 if ($this->cur_page == $loop)
176 {
177 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
178 }
179 else
180 {
181 $n = ($i == 0) ? '' : $i;
182 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
183 }
184 }
185 }
186
187 // Render the "next" link
188 if ($this->cur_page < $num_pages)
189 {
190 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
191 }
192
193 // Render the "Last" link
194 if (($this->cur_page + $this->num_links) < $num_pages)
195 {
196 $i = (($num_pages * $this->per_page) - $this->per_page);
197 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
198 }
199
200 // Kill double slashes. Note: Sometimes we can end up with a double slash
201 // in the penultimate link so we'll kill all double slashes.
202 $output = preg_replace("#([^:])//+#", "\\1/", $output);
203
204 // Add the wrapper HTML if exists
205 $output = $this->full_tag_open.$output.$this->full_tag_close;
206
207 return $output;
208 }
209}
210// END Pagination Class
adminb0dd10f2006-08-25 17:25:49 +0000211?>