Codeigniter Pagination class with bullets and images in the navigation


/ Published in: PHP
Save to your folder(s)

The standard codeigniter pagination class doesn't allow you to use images in the pagination navigation. I altered the default pagination class a bit. Now you can add the path to the images for the previous link, next link, digit and active digit in extra config parameters like so:

$this->load->library('pagination');
$config['base_url'] = base_url();
$config['total_rows'] = $this->your_model->select_items();
$config['per_page'] = '4';
$config['num_links'] = 10;
$config['full_tag_open'] = '';
$config['full_tag_close'] = '</p>';
$config['cur_page'] = $this->uri->segment(4);
//$config['cur_tag_open'] = "";
//$config['cur_tag_close'] = '';
$config['prev_link'] = "";
$config['next_link'] = "";
$config['digit'] = "";
$config['digit_active'] = "";

$this->pagination->initialize($config);
$this->mysmarty->assign("PAGINATION", $this->pagination->create_links());


Copy this code and paste it in your HTML
  1. <?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 ExpressionEngine Dev Team
  9.  * @copyright Copyright (c) 2008, EllisLab, Inc.
  10.  * @license http://codeigniter.com/user_guide/license.html
  11.  * @link http://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 ExpressionEngine Dev Team
  25.  * @link http://codeigniter.com/user_guide/libraries/pagination.html
  26.  */
  27. class 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;<strong>';
  46. var $cur_tag_close = '</strong>';
  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. var $page_query_string = FALSE;
  54. var $query_string_segment = 'per_page';
  55. var $digit = '';
  56. var $digit_active = '';
  57.  
  58.  
  59. /**
  60. * Constructor
  61. *
  62. * @access public
  63. * @param array initialization parameters
  64. */
  65. function CI_Pagination($params = array())
  66. {
  67. if (count($params) > 0)
  68. {
  69. $this->initialize($params);
  70. }
  71.  
  72. log_message('debug', "Pagination Class Initialized");
  73. }
  74.  
  75. // --------------------------------------------------------------------
  76.  
  77. /**
  78. * Initialize Preferences
  79. *
  80. * @access public
  81. * @param array initialization parameters
  82. * @return void
  83. */
  84. function initialize($params = array())
  85. {
  86. if (count($params) > 0)
  87. {
  88. foreach ($params as $key => $val)
  89. {
  90. if (isset($this->$key))
  91. {
  92. $this->$key = $val;
  93. }
  94. }
  95. }
  96. }
  97.  
  98. // --------------------------------------------------------------------
  99.  
  100. /**
  101. * Generate the pagination links
  102. *
  103. * @access public
  104. * @return string
  105. */
  106. function create_links()
  107. {
  108. // If our item count or per-page total is zero there is no need to continue.
  109. if ($this->total_rows == 0 OR $this->per_page == 0)
  110. {
  111. return '';
  112. }
  113.  
  114. // Calculate the total number of pages
  115. $num_pages = ceil($this->total_rows / $this->per_page);
  116.  
  117. // Is there only one page? Hm... nothing more to do here then.
  118. if ($num_pages == 1)
  119. {
  120. return '';
  121. }
  122.  
  123. // Determine the current page number.
  124. $CI =& get_instance();
  125.  
  126. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  127. {
  128. if ($CI->input->get($this->query_string_segment) != 0)
  129. {
  130. $this->cur_page = $CI->input->get($this->query_string_segment);
  131.  
  132. // Prep the current page - no funny business!
  133. $this->cur_page = (int) $this->cur_page;
  134. }
  135. }
  136. else
  137. {
  138. if ($CI->uri->segment($this->uri_segment) != 0)
  139. {
  140. $this->cur_page = $CI->uri->segment($this->uri_segment);
  141.  
  142. // Prep the current page - no funny business!
  143. $this->cur_page = (int) $this->cur_page;
  144. }
  145. }
  146.  
  147. $this->num_links = (int)$this->num_links;
  148.  
  149. if ($this->num_links < 1)
  150. {
  151. show_error('Your number of links must be a positive number.');
  152. }
  153.  
  154. if ( ! is_numeric($this->cur_page))
  155. {
  156. $this->cur_page = 0;
  157. }
  158.  
  159. // Is the page number beyond the result range?
  160. // If so we show the last page
  161. if ($this->cur_page > $this->total_rows)
  162. {
  163. $this->cur_page = ($num_pages - 1) * $this->per_page;
  164. }
  165.  
  166. $uri_page_number = $this->cur_page;
  167. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  168.  
  169. // Calculate the start and end numbers. These determine
  170. // which number to start and end the digit links with
  171. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  172. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  173.  
  174. // Is pagination being used over GET or POST? If get, add a per_page query
  175. // string. If post, add a trailing slash to the base URL if needed
  176. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  177. {
  178. $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  179. }
  180. else
  181. {
  182. $this->base_url = rtrim($this->base_url, '/') .'/';
  183. }
  184.  
  185. // And here we go...
  186. $output = '';
  187.  
  188. // Render the "First" link
  189. if ($this->cur_page > ($this->num_links + 1))
  190. {
  191. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  192. }
  193.  
  194. // Render the "previous" link
  195. if ($this->cur_page != 1)
  196. {
  197. $i = $uri_page_number - $this->per_page;
  198. if ($i == 0) $i = '';
  199. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  200. }
  201.  
  202. // Write the digit links
  203. for ($loop = $start -1; $loop <= $end; $loop++)
  204. {
  205. $i = ($loop * $this->per_page) - $this->per_page;
  206.  
  207. if ($i >= 0)
  208. {
  209.  
  210. if ($this->cur_page == $loop)
  211. {
  212. if($this->digit_active!=""){
  213. $output .= $this->cur_tag_open.$this->digit_active.$this->cur_tag_close; // Current page
  214. }else{
  215. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  216. }
  217. }
  218. else
  219. {
  220. $n = ($i == 0) ? '' : $i;
  221. if($this->digit!=""){
  222. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$this->digit.'</a>'.$this->num_tag_close;
  223. }else{
  224. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
  225. }
  226. }
  227. }
  228. }
  229.  
  230. // Render the "next" link
  231. if ($this->cur_page < $num_pages)
  232. {
  233. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
  234. }
  235.  
  236. // Render the "Last" link
  237. if (($this->cur_page + $this->num_links) < $num_pages)
  238. {
  239. $i = (($num_pages * $this->per_page) - $this->per_page);
  240. $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  241. }
  242.  
  243. // Kill double slashes. Note: Sometimes we can end up with a double slash
  244. // in the penultimate link so we'll kill all double slashes.
  245. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  246.  
  247. // Add the wrapper HTML if exists
  248. $output = $this->full_tag_open.$output.$this->full_tag_close;
  249.  
  250. return $output;
  251. }
  252. }
  253. // END Pagination Class
  254.  
  255. /* End of file Pagination.php */
  256. /* Location: ./system/libraries/Pagination.php */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.