blob: a004419d656979be16d9bb8bcdfd9f721ee32e2d [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 /*
admin10c3f412006-10-08 07:21:12 +0000113 // Calculate the total number of pages
114 $num_pages = intval($this->total_rows / $this->per_page);
115
116 // Use modulus to see if our division has a remainder. If so, add one to our page number.
admine334c472006-10-21 19:44:22 +0000117 if ($this->total_rows % $this->per_page)
admin10c3f412006-10-08 07:21:12 +0000118 {
119 $num_pages++;
120 }
121 */
admine334c472006-10-21 19:44:22 +0000122
123 // Is there only one page? Hm... nothing more to do here then.
124 if ($num_pages == 1)
125 {
126 return '';
127 }
128
adminb0dd10f2006-08-25 17:25:49 +0000129 // Determine the current page number.
admin88a8ad12006-10-07 03:16:32 +0000130 $CI =& get_instance();
131 if ($CI->uri->segment($this->uri_segment) != 0)
adminb0dd10f2006-08-25 17:25:49 +0000132 {
admin88a8ad12006-10-07 03:16:32 +0000133 $this->cur_page = $CI->uri->segment($this->uri_segment);
adminb0dd10f2006-08-25 17:25:49 +0000134 }
135
admin1cf89aa2006-09-03 18:24:39 +0000136 if ( ! is_numeric($this->cur_page))
adminb0dd10f2006-08-25 17:25:49 +0000137 {
138 $this->cur_page = 0;
139 }
140
141 $uri_page_number = $this->cur_page;
142 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
admine334c472006-10-21 19:44:22 +0000143
adminb0dd10f2006-08-25 17:25:49 +0000144 // Calculate the start and end numbers. These determine
145 // which number to start and end the digit links with
admine334c472006-10-21 19:44:22 +0000146 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
147 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
148
adminb0dd10f2006-08-25 17:25:49 +0000149 // Add a trailing slash to the base URL if needed
150 $this->base_url = preg_replace("/(.+?)\/*$/", "\\1/", $this->base_url);
151
152 // And here we go...
admine334c472006-10-21 19:44:22 +0000153 $output = '';
154
adminb0dd10f2006-08-25 17:25:49 +0000155 // Render the "First" link
admine334c472006-10-21 19:44:22 +0000156 if ($this->cur_page > $this->num_links)
157 {
158 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
159 }
160
adminb0dd10f2006-08-25 17:25:49 +0000161 // Render the "previous" link
admine334c472006-10-21 19:44:22 +0000162 if (($this->cur_page - $this->num_links) >= 0)
163 {
164 $i = $uri_page_number - $this->per_page;
165 if ($i == 0) $i = '';
166 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
167 }
168
adminb0dd10f2006-08-25 17:25:49 +0000169 // Write the digit links
admine334c472006-10-21 19:44:22 +0000170 for ($loop = $start -1; $loop <= $end; $loop++)
171 {
adminb0dd10f2006-08-25 17:25:49 +0000172 $i = ($loop * $this->per_page) - $this->per_page;
173
174 if ($i >= 0)
175 {
176 if ($this->cur_page == $loop)
177 {
178 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
179 }
180 else
181 {
182 $n = ($i == 0) ? '' : $i;
183 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
184 }
185 }
admine334c472006-10-21 19:44:22 +0000186 }
adminb0dd10f2006-08-25 17:25:49 +0000187
188 // Render the "next" link
admine334c472006-10-21 19:44:22 +0000189 if ($this->cur_page < $num_pages)
190 {
191 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
192 }
adminb0dd10f2006-08-25 17:25:49 +0000193
194 // Render the "Last" link
admine334c472006-10-21 19:44:22 +0000195 if (($this->cur_page + $this->num_links) < $num_pages)
196 {
197 $i = (($num_pages * $this->per_page) - $this->per_page);
198 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
199 }
200
201 // Kill double slashes. Note: Sometimes we can end up with a double slash
adminbd6bee72006-10-21 19:39:00 +0000202 // in the penultimate link so we'll kill all double slashes.
admine334c472006-10-21 19:44:22 +0000203 $output = preg_replace("#([^:])//+#", "\\1/", $output);
adminb0dd10f2006-08-25 17:25:49 +0000204
205 // Add the wrapper HTML if exists
206 $output = $this->full_tag_open.$output.$this->full_tag_close;
207
208 return $output;
admine334c472006-10-21 19:44:22 +0000209 }
adminb0dd10f2006-08-25 17:25:49 +0000210}
211// END Pagination Class
212?>