blob: 07ad6a683bbe26fe16d274a16d256c7e1a88388c [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.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @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)
admine334c472006-10-21 19:44:22 +000031 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
adminb0dd10f2006-08-25 17:25:49 +000034 var $first_link = '&lsaquo; First';
35 var $next_link = '&gt;';
36 var $prev_link = '&lt;';
admine334c472006-10-21 19:44:22 +000037 var $last_link = 'Last &rsaquo;';
adminb0dd10f2006-08-25 17:25:49 +000038 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 */
admine334c472006-10-21 19:44:22 +000060 function CI_Pagination($params = array())
61 {
adminb0dd10f2006-08-25 17:25:49 +000062 if (count($params) > 0)
63 {
64 $this->initialize($params);
65 }
66
67 log_message('debug', "Pagination Class Initialized");
admine334c472006-10-21 19:44:22 +000068 }
adminb0dd10f2006-08-25 17:25:49 +000069
70 // --------------------------------------------------------------------
71
72 /**
73 * Initialize Preferences
74 *
75 * @access public
76 * @param array initialization parameters
77 * @return void
78 */
admine334c472006-10-21 19:44:22 +000079 function initialize($params = array())
80 {
adminb0dd10f2006-08-25 17:25:49 +000081 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 }
admine334c472006-10-21 19:44:22 +000091 }
adminb0dd10f2006-08-25 17:25:49 +000092
93 // --------------------------------------------------------------------
94
95 /**
96 * Generate the pagination links
97 *
98 * @access public
99 * @return string
100 */
admine334c472006-10-21 19:44:22 +0000101 function create_links()
102 {
adminb0dd10f2006-08-25 17:25:49 +0000103 // If our item count or per-page total is zero there is no need to continue.
admine334c472006-10-21 19:44:22 +0000104 if ($this->total_rows == 0 OR $this->per_page == 0)
105 {
106 return '';
107 }
admin10c3f412006-10-08 07:21:12 +0000108
adminb0dd10f2006-08-25 17:25:49 +0000109 // Calculate the total number of pages
admine334c472006-10-21 19:44:22 +0000110 $num_pages = ceil($this->total_rows / $this->per_page);
admin10c3f412006-10-08 07:21:12 +0000111
admine334c472006-10-21 19:44:22 +0000112 // Is there only one page? Hm... nothing more to do here then.
113 if ($num_pages == 1)
114 {
115 return '';
116 }
117
adminb0dd10f2006-08-25 17:25:49 +0000118 // Determine the current page number.
admin88a8ad12006-10-07 03:16:32 +0000119 $CI =& get_instance();
120 if ($CI->uri->segment($this->uri_segment) != 0)
adminb0dd10f2006-08-25 17:25:49 +0000121 {
admin88a8ad12006-10-07 03:16:32 +0000122 $this->cur_page = $CI->uri->segment($this->uri_segment);
adminb93464d2006-10-31 00:36:32 +0000123
124 // Prep the current page - no funny business!
125 $this->cur_page = preg_replace("/[a-z\-]/", "", $this->cur_page);
adminb0dd10f2006-08-25 17:25:49 +0000126 }
adminb93464d2006-10-31 00:36:32 +0000127
admin1cf89aa2006-09-03 18:24:39 +0000128 if ( ! is_numeric($this->cur_page))
adminb0dd10f2006-08-25 17:25:49 +0000129 {
130 $this->cur_page = 0;
131 }
132
adminb84f7c02006-10-27 03:11:11 +0000133 // 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
adminb0dd10f2006-08-25 17:25:49 +0000140 $uri_page_number = $this->cur_page;
141 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
admine334c472006-10-21 19:44:22 +0000142
adminb0dd10f2006-08-25 17:25:49 +0000143 // Calculate the start and end numbers. These determine
144 // which number to start and end the digit links with
admine334c472006-10-21 19:44:22 +0000145 $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
adminb0dd10f2006-08-25 17:25:49 +0000148 // 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...
admine334c472006-10-21 19:44:22 +0000152 $output = '';
153
adminb0dd10f2006-08-25 17:25:49 +0000154 // Render the "First" link
admine334c472006-10-21 19:44:22 +0000155 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
adminb0dd10f2006-08-25 17:25:49 +0000160 // Render the "previous" link
admine334c472006-10-21 19:44:22 +0000161 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
adminb0dd10f2006-08-25 17:25:49 +0000168 // Write the digit links
admine334c472006-10-21 19:44:22 +0000169 for ($loop = $start -1; $loop <= $end; $loop++)
170 {
adminb0dd10f2006-08-25 17:25:49 +0000171 $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 }
admine334c472006-10-21 19:44:22 +0000185 }
adminb0dd10f2006-08-25 17:25:49 +0000186
187 // Render the "next" link
admine334c472006-10-21 19:44:22 +0000188 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 }
adminb0dd10f2006-08-25 17:25:49 +0000192
193 // Render the "Last" link
admine334c472006-10-21 19:44:22 +0000194 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
adminbd6bee72006-10-21 19:39:00 +0000201 // in the penultimate link so we'll kill all double slashes.
admine334c472006-10-21 19:44:22 +0000202 $output = preg_replace("#([^:])//+#", "\\1/", $output);
adminb0dd10f2006-08-25 17:25:49 +0000203
204 // Add the wrapper HTML if exists
205 $output = $this->full_tag_open.$output.$this->full_tag_close;
206
207 return $output;
admine334c472006-10-21 19:44:22 +0000208 }
adminb0dd10f2006-08-25 17:25:49 +0000209}
210// END Pagination Class
211?>