MY_pagination.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. if (!defined('BASEPATH')) {
  3. exit('No direct script access allowed');
  4. }
  5. require_once SYSDIR."/libraries/Pagination.php";
  6. class MY_pagination extends CI_Pagination{
  7. protected $page_count;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. }
  12. /**
  13. * 获取总页数
  14. */
  15. public function get_page_count(){
  16. return $this->page_count;
  17. }
  18. /**
  19. * 生成分页连接
  20. *
  21. * @return string
  22. */
  23. public function create_pages()
  24. {
  25. $this->page_count = (int) ceil($this->total_rows / $this->per_page);
  26. if ($this->total_rows == 0 OR $this->per_page == 0)
  27. {
  28. return '';
  29. }
  30. // Calculate the total number of pages
  31. $num_pages = $this->page_count;
  32. // Check the user defined number of links.
  33. $this->num_links = (int) $this->num_links;
  34. if ($this->num_links < 0)
  35. {
  36. show_error('Your number of links must be a non-negative number.');
  37. }
  38. // Keep any existing query string items.
  39. // Note: Has nothing to do with any other query string option.
  40. if ($this->reuse_query_string === TRUE)
  41. {
  42. $get = $this->CI->input->get();
  43. // Unset the control, method, old-school routing options
  44. unset($get['c'], $get['m'], $get[$this->query_string_segment]);
  45. }
  46. else
  47. {
  48. $get = array();
  49. }
  50. // Put together our base and first URLs.
  51. // Note: DO NOT append to the properties as that would break successive calls
  52. $base_url = trim($this->base_url);
  53. $first_url = $this->first_url;
  54. $query_string = '';
  55. $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
  56. // Are we using query strings?
  57. if ($this->page_query_string === TRUE)
  58. {
  59. // If a custom first_url hasn't been specified, we'll create one from
  60. // the base_url, but without the page item.
  61. if ($first_url === '')
  62. {
  63. $first_url = $base_url;
  64. // If we saved any GET items earlier, make sure they're appended.
  65. if ( ! empty($get))
  66. {
  67. $first_url .= $query_string_sep.http_build_query($get);
  68. }
  69. }
  70. // Add the page segment to the end of the query string, where the
  71. // page number will be appended.
  72. $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
  73. }
  74. else
  75. {
  76. // Standard segment mode.
  77. // Generate our saved query string to append later after the page number.
  78. if ( ! empty($get))
  79. {
  80. $query_string = $query_string_sep.http_build_query($get);
  81. $this->suffix .= $query_string;
  82. }
  83. // Does the base_url have the query string in it?
  84. // If we're supposed to save it, remove it so we can append it later.
  85. if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
  86. {
  87. $base_url = substr($base_url, 0, $base_query_pos);
  88. }
  89. if ($first_url === '')
  90. {
  91. $first_url = $base_url.$query_string;
  92. }
  93. $base_url = rtrim($base_url, '/').'/';
  94. }
  95. // Determine the current page number.
  96. $base_page = ($this->use_page_numbers) ? 1 : 0;
  97. // Are we using query strings?
  98. if ($this->page_query_string === TRUE)
  99. {
  100. $this->cur_page = $this->CI->input->get($this->query_string_segment);
  101. }
  102. elseif (empty($this->cur_page))
  103. {
  104. // Default to the last segment number if one hasn't been defined.
  105. if ($this->uri_segment === 0)
  106. {
  107. $this->uri_segment = count($this->CI->uri->segment_array());
  108. }
  109. $this->cur_page = $this->CI->uri->segment($this->uri_segment);
  110. // Remove any specified prefix/suffix from the segment.
  111. if ($this->prefix !== '' OR $this->suffix !== '')
  112. {
  113. $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
  114. }
  115. }
  116. else
  117. {
  118. $this->cur_page = (string) $this->cur_page;
  119. }
  120. // If something isn't quite right, back to the default base page.
  121. if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
  122. {
  123. $this->cur_page = $base_page;
  124. }
  125. else
  126. {
  127. // Make sure we're using integers for comparisons later.
  128. $this->cur_page = (int) $this->cur_page;
  129. }
  130. // Is the page number beyond the result range?
  131. // If so, we show the last page.
  132. if ($this->use_page_numbers)
  133. {
  134. if ($this->cur_page > $num_pages)
  135. {
  136. $this->cur_page = $num_pages;
  137. }
  138. }
  139. elseif ($this->cur_page > $this->total_rows)
  140. {
  141. $this->cur_page = ($num_pages - 1) * $this->per_page;
  142. }
  143. $uri_page_number = $this->cur_page;
  144. // If we're using offset instead of page numbers, convert it
  145. // to a page number, so we can generate the surrounding number links.
  146. if ( ! $this->use_page_numbers)
  147. {
  148. $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
  149. }
  150. // Calculate the start and end numbers. These determine
  151. // which number to start and end the digit links with.
  152. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  153. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  154. // And here we go...
  155. $output = '';
  156. // Render the "First" link.
  157. if ($this->first_link !== FALSE )
  158. {
  159. // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
  160. if($this->cur_page == 1){
  161. $output .= '<li class="disabled"><a href="javascript:;" style="line-height: 26px">'
  162. . $this->first_link . '</a>' . $this->first_tag_close;
  163. }else {
  164. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
  165. $output .= $this->first_tag_open . '<a style="line-height: 26px" href="' . $first_url . '"' . $attributes . $this->_attr_rel('start') . '>'
  166. . $this->first_link . '</a>' . $this->first_tag_close;
  167. }
  168. }
  169. // Render the "Previous" link.
  170. if ($this->prev_link !== FALSE )
  171. {
  172. if($this->cur_page == 1){
  173. $output .= '<li class="disabled"><a href="javascript:;">'
  174. . $this->prev_link . '</a>' . $this->prev_tag_close;
  175. }else {
  176. $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
  177. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
  178. if ($i === $base_page) {
  179. // First page
  180. $output .= $this->prev_tag_open . '<a href="' . $first_url . '"' . $attributes . $this->_attr_rel('prev') . '>'
  181. . $this->prev_link . '</a>' . $this->prev_tag_close;
  182. } else {
  183. $append = $this->prefix . $i . $this->suffix;
  184. $output .= $this->prev_tag_open . '<a href="' . $base_url . $append . '"' . $attributes . $this->_attr_rel('prev') . '>'
  185. . $this->prev_link . '</a>' . $this->prev_tag_close;
  186. }
  187. }
  188. }
  189. // Render the pages
  190. if ($this->display_pages !== FALSE)
  191. {
  192. // Write the digit links
  193. for ($loop = $start - 1; $loop <= $end; $loop++)
  194. {
  195. $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
  196. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
  197. if ($i >= $base_page)
  198. {
  199. if ($this->cur_page === $loop)
  200. {
  201. // Current page
  202. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
  203. }
  204. elseif ($i === $base_page)
  205. {
  206. // First page
  207. $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
  208. .$loop.'</a>'.$this->num_tag_close;
  209. }
  210. else
  211. {
  212. $append = $this->prefix.$i.$this->suffix;
  213. $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
  214. .$loop.'</a>'.$this->num_tag_close;
  215. }
  216. }
  217. }
  218. }
  219. // Render the "next" link
  220. if ($this->next_link !== FALSE)
  221. {
  222. $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
  223. if($this->cur_page == $num_pages){
  224. $output .= '<li class="disabled"><a href="javascript:;" >' . $this->next_link . '</a>' . $this->next_tag_close;
  225. }else {
  226. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
  227. $output .= $this->next_tag_open . '<a href="' . $base_url . $this->prefix . $i . $this->suffix . '"' . $attributes
  228. . $this->_attr_rel('next') . '>' . $this->next_link . '</a>' . $this->next_tag_close;
  229. }
  230. }
  231. // Render the "Last" link
  232. if ($this->last_link !== FALSE)
  233. {
  234. if($this->cur_page == $num_pages) {
  235. $output .= '<li class="disabled"><a href="javascript:;" style="line-height: 26px">'. $this->last_link . '</a>' . $this->last_tag_close;
  236. }else {
  237. $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
  238. $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
  239. $output .= $this->last_tag_open . '<a style="line-height: 26px" href="' . $base_url . $this->prefix . $i . $this->suffix . '"' . $attributes . '>'
  240. . $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. // Add the wrapper HTML if exists
  247. return $this->full_tag_open.$output.$this->full_tag_close;
  248. }
  249. }