Просмотр файла mc-2.7.0/libraries/pagination.php

Размер файла: 7.07Kb
  1. <?php
  2.  
  3. /**
  4. * CodeIgniter
  5. *
  6. * An open source application development framework for PHP 4.3.2 or newer
  7. *
  8. * @package CodeIgniter
  9. * @author ExpressionEngine Dev Team
  10. * @copyright Copyright (c) 2008, EllisLab, Inc.
  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. /**
  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 = '&lt;&lt;';
  35. var $next_link = '&gt;';
  36. var $prev_link = '&lt;';
  37. var $last_link = '&gt;&gt;';
  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 = ' ... ';
  43. var $last_tag_open = ' ... ';
  44. var $last_tag_close = '';
  45. var $cur_tag_open = ' <strong>';
  46. var $cur_tag_close = '</strong>';
  47. var $next_tag_open = ' ';
  48. var $next_tag_close = ' ';
  49. var $prev_tag_open = ' ';
  50. var $prev_tag_close = '';
  51. var $num_tag_open = ' ';
  52. var $num_tag_close = ',';
  53. var $page_query_string = FALSE;
  54. var $query_string_segment = 'start';
  55.  
  56. /**
  57. * Constructor
  58. *
  59. * @access public
  60. * @param array initialization parameters
  61. */
  62. function CI_Pagination($params = array()) {
  63. if (count($params) > 0) {
  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. if (count($params) > 0) {
  81. foreach ($params as $key => $val) {
  82. if (isset($this->$key)) {
  83. $this->$key = $val;
  84. }
  85. }
  86. }
  87. }
  88.  
  89. // --------------------------------------------------------------------
  90.  
  91. /**
  92. * Generate the pagination links
  93. *
  94. * @access public
  95. * @return string
  96. */
  97. function create_links() {
  98. $start = $_GET['start'];
  99. $start = is_numeric($_GET['page']) ? $_GET['page'] * $this->per_page - 1 : $start;
  100. // If our item count or per-page total is zero there is no need to continue.
  101. if ($this->total_rows == 0 OR $this->per_page == 0) {
  102. return '';
  103. }
  104.  
  105. // Calculate the total number of pages
  106. $num_pages = ceil($this->total_rows / $this->per_page);
  107.  
  108. // Is there only one page? Hm... nothing more to do here then.
  109. if ($num_pages == 1) {
  110. return '';
  111. }
  112.  
  113. $this->cur_page = $start;
  114.  
  115. // Prep the current page - no funny business!
  116. $this->cur_page = (int) $this->cur_page;
  117.  
  118.  
  119. $this->num_links = (int) $this->num_links;
  120.  
  121. if ($this->num_links < 1) {
  122. a_error('Your number of links must be a positive number.');
  123. }
  124.  
  125. if (!is_numeric($this->cur_page)) {
  126. $this->cur_page = 0;
  127. }
  128.  
  129. // Is the page number beyond the result range?
  130. // If so we show the last page
  131. if ($this->cur_page > $this->total_rows) {
  132. $this->cur_page = ($num_pages - 1) * $this->per_page;
  133. }
  134.  
  135. $uri_page_number = $this->cur_page;
  136. $this->cur_page = floor(($this->cur_page / $this->per_page) + 1);
  137.  
  138. // Calculate the start and end numbers. These determine
  139. // which number to start and end the digit links with
  140. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  141. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  142.  
  143. // And here we go...
  144. $output = '';
  145.  
  146. // Render the "First" link
  147. if (!empty($this->first_link)) {
  148. if ($this->cur_page > ($this->num_links + 1)) {
  149. $output .= $this->first_tag_open . '<a href="' . $this->base_url . '">1</a>' . $this->first_tag_close;
  150. }
  151. #else $output .= $this->first_tag_open . $this->first_link . $this->first_tag_close;;
  152. }
  153.  
  154. /* / Render the "previous" link
  155. if(!empty($this->prev_link)) {
  156. if ($this->cur_page != 1)
  157. {
  158. $i = $uri_page_number - $this->per_page;
  159. if ($i == 0) $i = '';
  160. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  161. }
  162. else $output .= $this->prev_tag_open . $this->prev_link . $this->prev_tag_close;
  163. }
  164. */
  165.  
  166. // Write the digit links
  167. for ($loop = $start - 1; $loop <= $end; $loop++) {
  168. $i = ($loop * $this->per_page) - $this->per_page;
  169.  
  170. if ($i >= 0) {
  171. if ($this->cur_page == $loop) {
  172. $output .= $this->cur_tag_open . $loop . $this->cur_tag_close . ($loop != $num_pages ? ',' : ''); // Current page
  173. } else {
  174. $n = ($i == 0) ? '' : $i;
  175. $output .= $this->num_tag_open . '<a href="' . $this->base_url . $n . '">' . $loop . '</a>' . ($loop != $num_pages ? $this->num_tag_close : '');
  176. }
  177. }
  178. }
  179.  
  180. /* / Render the "next" link
  181. if(!empty($this->next_link)) {
  182. if ($this->cur_page < $num_pages)
  183. {
  184. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
  185. }
  186. else $output .= $this->next_tag_open . $this->next_link . $this->next_tag_close;
  187. }
  188. */
  189.  
  190. // Render the "Last" link
  191. if (!empty($this->last_link)) {
  192. if (($this->cur_page + $this->num_links) < $num_pages) {
  193. $i = (($num_pages * $this->per_page) - $this->per_page);
  194. $output .= $this->last_tag_open . '<a href="' . $this->base_url . $i . '">' . $num_pages . '</a>' . $this->last_tag_close;
  195. }
  196. #else $output .= $this->last_tag_open . $this->last_link . $this->last_tag_close;
  197. }
  198.  
  199. // Kill double slashes. Note: Sometimes we can end up with a double slash
  200. // in the penultimate link so we'll kill all double slashes.
  201. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  202.  
  203. // Add the wrapper HTML if exists
  204. $output = $this->full_tag_open . $output . $this->full_tag_close;
  205.  
  206. return $output;
  207. }
  208.  
  209. }
  210.  
  211. // END Pagination Class
  212.  
  213. /* End of file Pagination.php */
  214. /* Location: ./system/libraries/Pagination.php */