Pagination.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Pagination Class
  19. *
  20. * @package CodeIgniter
  21. * @subpackage Libraries
  22. * @category Pagination
  23. * @author EllisLab Dev Team
  24. * @link http://codeigniter.com/user_guide/libraries/pagination.html
  25. */
  26. class CI_Pagination {
  27. var $base_url = ''; // The page we are linking to
  28. var $prefix = ''; // A custom prefix added to the path.
  29. var $suffix = ''; // A custom suffix added to the path.
  30. var $total_rows = 0; // 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 $use_page_numbers = FALSE; // Use page number for segment instead of offset
  35. var $first_link = '&lsaquo; First';
  36. var $next_link = '&gt;';
  37. var $prev_link = '&lt;';
  38. var $last_link = 'Last &rsaquo;';
  39. var $uri_segment = 3;
  40. var $full_tag_open = '';
  41. var $full_tag_close = '';
  42. var $first_tag_open = '';
  43. var $first_tag_close = '&nbsp;';
  44. var $last_tag_open = '&nbsp;';
  45. var $last_tag_close = '';
  46. var $first_url = ''; // Alternative URL for the First Page.
  47. var $cur_tag_open = '&nbsp;<strong>';
  48. var $cur_tag_close = '</strong>';
  49. var $next_tag_open = '&nbsp;';
  50. var $next_tag_close = '&nbsp;';
  51. var $prev_tag_open = '&nbsp;';
  52. var $prev_tag_close = '';
  53. var $num_tag_open = '&nbsp;';
  54. var $num_tag_close = '';
  55. var $page_query_string = FALSE;
  56. var $query_string_segment = 'per_page';
  57. var $display_pages = TRUE;
  58. var $anchor_class = '';
  59. /**
  60. * Constructor
  61. *
  62. * @access public
  63. * @param array initialization parameters
  64. */
  65. public function __construct($params = array())
  66. {
  67. if (count($params) > 0)
  68. {
  69. $this->initialize($params);
  70. }
  71. if ($this->anchor_class != '')
  72. {
  73. $this->anchor_class = 'class="'.$this->anchor_class.'" ';
  74. }
  75. log_message('debug', "Pagination Class Initialized");
  76. }
  77. // --------------------------------------------------------------------
  78. /**
  79. * Initialize Preferences
  80. *
  81. * @access public
  82. * @param array initialization parameters
  83. * @return void
  84. */
  85. function initialize($params = array())
  86. {
  87. if (count($params) > 0)
  88. {
  89. foreach ($params as $key => $val)
  90. {
  91. if (isset($this->$key))
  92. {
  93. $this->$key = $val;
  94. }
  95. }
  96. }
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Generate the pagination links
  101. *
  102. * @access public
  103. * @return string
  104. */
  105. function create_links()
  106. {
  107. // If our item count or per-page total is zero there is no need to continue.
  108. if ($this->total_rows == 0 OR $this->per_page == 0)
  109. {
  110. return '';
  111. }
  112. // Calculate the total number of pages
  113. $num_pages = ceil($this->total_rows / $this->per_page);
  114. // Is there only one page? Hm... nothing more to do here then.
  115. if ($num_pages == 1)
  116. {
  117. return '';
  118. }
  119. // Set the base page index for starting page number
  120. if ($this->use_page_numbers)
  121. {
  122. $base_page = 1;
  123. }
  124. else
  125. {
  126. $base_page = 0;
  127. }
  128. // Determine the current page number.
  129. $CI =& get_instance();
  130. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  131. {
  132. if ($CI->input->get($this->query_string_segment) != $base_page)
  133. {
  134. $this->cur_page = $CI->input->get($this->query_string_segment);
  135. // Prep the current page - no funny business!
  136. $this->cur_page = (int) $this->cur_page;
  137. }
  138. }
  139. else
  140. {
  141. if ($CI->uri->segment($this->uri_segment) != $base_page)
  142. {
  143. $this->cur_page = $CI->uri->segment($this->uri_segment);
  144. // Prep the current page - no funny business!
  145. $this->cur_page = (int) $this->cur_page;
  146. }
  147. }
  148. // Set current page to 1 if using page numbers instead of offset
  149. if ($this->use_page_numbers AND $this->cur_page == 0)
  150. {
  151. $this->cur_page = $base_page;
  152. }
  153. $this->num_links = (int)$this->num_links;
  154. if ($this->num_links < 1)
  155. {
  156. show_error('Your number of links must be a positive number.');
  157. }
  158. if ( ! is_numeric($this->cur_page))
  159. {
  160. $this->cur_page = $base_page;
  161. }
  162. // Is the page number beyond the result range?
  163. // If so we show the last page
  164. if ($this->use_page_numbers)
  165. {
  166. if ($this->cur_page > $num_pages)
  167. {
  168. $this->cur_page = $num_pages;
  169. }
  170. }
  171. else
  172. {
  173. if ($this->cur_page > $this->total_rows)
  174. {
  175. $this->cur_page = ($num_pages - 1) * $this->per_page;
  176. }
  177. }
  178. $uri_page_number = $this->cur_page;
  179. if ( ! $this->use_page_numbers)
  180. {
  181. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  182. }
  183. // Calculate the start and end numbers. These determine
  184. // which number to start and end the digit links with
  185. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  186. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  187. // Is pagination being used over GET or POST? If get, add a per_page query
  188. // string. If post, add a trailing slash to the base URL if needed
  189. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  190. {
  191. $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  192. }
  193. else
  194. {
  195. $this->base_url = rtrim($this->base_url, '/') .'/';
  196. }
  197. // And here we go...
  198. $output = '';
  199. // Render the "First" link
  200. if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
  201. {
  202. $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
  203. $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  204. }
  205. // Render the "previous" link
  206. if ($this->prev_link !== FALSE AND $this->cur_page != 1)
  207. {
  208. if ($this->use_page_numbers)
  209. {
  210. $i = $uri_page_number - 1;
  211. }
  212. else
  213. {
  214. $i = $uri_page_number - $this->per_page;
  215. }
  216. if ($i == 0 && $this->first_url != '')
  217. {
  218. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  219. }
  220. else
  221. {
  222. $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
  223. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  224. }
  225. }
  226. // Render the pages
  227. if ($this->display_pages !== FALSE)
  228. {
  229. // Write the digit links
  230. for ($loop = $start -1; $loop <= $end; $loop++)
  231. {
  232. if ($this->use_page_numbers)
  233. {
  234. $i = $loop;
  235. }
  236. else
  237. {
  238. $i = ($loop * $this->per_page) - $this->per_page;
  239. }
  240. if ($i >= $base_page)
  241. {
  242. if ($this->cur_page == $loop)
  243. {
  244. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  245. }
  246. else
  247. {
  248. $n = ($i == $base_page) ? '' : $i;
  249. if ($n == '' && $this->first_url != '')
  250. {
  251. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
  252. }
  253. else
  254. {
  255. $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
  256. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
  257. }
  258. }
  259. }
  260. }
  261. }
  262. // Render the "next" link
  263. if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
  264. {
  265. if ($this->use_page_numbers)
  266. {
  267. $i = $this->cur_page + 1;
  268. }
  269. else
  270. {
  271. $i = ($this->cur_page * $this->per_page);
  272. }
  273. $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  274. }
  275. // Render the "Last" link
  276. if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
  277. {
  278. if ($this->use_page_numbers)
  279. {
  280. $i = $num_pages;
  281. }
  282. else
  283. {
  284. $i = (($num_pages * $this->per_page) - $this->per_page);
  285. }
  286. $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  287. }
  288. // Kill double slashes. Note: Sometimes we can end up with a double slash
  289. // in the penultimate link so we'll kill all double slashes.
  290. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  291. // Add the wrapper HTML if exists
  292. $output = $this->full_tag_open.$output.$this->full_tag_close;
  293. return $output;
  294. }
  295. }
  296. // END Pagination Class
  297. /* End of file Pagination.php */
  298. /* Location: ./system/libraries/Pagination.php */