php codeigniter框架分页类

codeigniter 具有非常容易使用的分页类,在本教程中我会做一个从数据库教程返回一组结果并分页这些结果的简单例子,我将使用最新版本的 ci,分页类并没有修改,至少我认为没有,用最新的稳定版框架总是好的.

调用方法,代码如下:

  1. //创建分页
  2. $config = array();
  3. $this->load->library('hpages');
  4. $config['base_url'] = "channel/lists/c{$slug}/{page}";
  5. $config['total_rows'] = intval($total);
  6. $config['per_page'] = $pagesize;
  7. //开源代码phpfensi.com
  8. $config['uri_segment'] = 1;
  9. $config['num_links'] = 3;
  10. $config['underline_uri_seg'] = 1; //下划线uri中页数所在的位置
  11. $this->hpages->init($config);
  12. $this->template['lists'] = $list;
  13. $this->template['pagestr'] = $this->hpages->create_links(1);

php文件代码,实例如下:

  1. <?php if (! defined('basepath')) exit('access denied!');
  2. /**
  3. * file_name : hpages.php
  4. * 浩海网络 前台 分页类
  5. *
  6. * @package haohailuo
  7. * @author by laurence.xu <haohailuo@163.com>
  8. * @copyright copyright (c) 2010, haohailuo, inc.
  9. * @link http://www.haohailuo.com
  10. * @since version 1.0 $id$
  11. * @version wed dec 08 12:21:17 cst 2010
  12. * @filesource
  13. */
  14. class hpages {
  15. var $base_url = ''; //基本链接地址
  16. var $total_rows = ''; //数据总数
  17. var $per_page = 10; //每页条数
  18. var $num_links = 2; //要显示的左右链接的个数
  19. var $cur_page = 1; //当前页数
  20. var $first_link = '‹ first'; //首页字符
  21. var $next_link = '>'; //下一页的字符
  22. var $prev_link = '<'; //上一页的字符
  23. var $last_link = 'last ›'; //末页的字符
  24. var $uri_segment = 3; //分页数所在的uri片段位置
  25. var $full_tag_open = ''; //分页区域开始的html标签
  26. var $full_tag_close = ''; //分页区域结束的后html标签
  27. var $first_tag_open = ''; //首页开始的html标签
  28. var $first_tag_close = ' '; //首页结束的html标签
  29. var $last_tag_open = ' '; //末页开始的html标签
  30. var $last_tag_close = ''; //末页结束的html标签
  31. var $cur_tag_open = ' <b>';//当前页开始的...
  32. var $cur_tag_close = '</b>'; //当前页结束的...
  33. var $next_tag_open = ' '; //下一页开始的.....
  34. var $next_tag_close = ' '; //下一页结束的.....
  35. var $prev_tag_open = ' '; //上一页开始的.....
  36. var $prev_tag_close = ''; //上一页结束的.....
  37. var $num_tag_open = ' '; //“数字”链接的打开标签。
  38. var $num_tag_close = ''; //“数字”链接的结束标签。
  39. var $page_query_string = false;
  40. var $query_string_segment = 'per_page';
  41. var $page_mode = 'default'; //default for add page at the end? if include {page}, will replace it for current page.
  42. var $underline_uri_seg = -1; //存在下划线时,页码所在数组下标位置
  43. var $custom_cur_page = 0; //自定义当前页码,存在此值是,系统将不自动判断当前页数,默认不启用
  44. function __construct() {
  45. $this->hpages();
  46. }
  47. /**
  48. * constructor
  49. *
  50. * @access public
  51. */
  52. function hpages() {
  53. if (file_exists(apppath.'config/pagination.php')) {
  54. require_once(apppath.'config/pagination.php');
  55. foreach ($config as $key=>$val) {
  56. $this->{$key} = $val;
  57. }
  58. }
  59. log_message('debug', "hpages class initialized");
  60. }
  61. /**
  62. * 初始化参数
  63. *
  64. * @see init()
  65. * @author laurence.xu <haohailuo@163.com>
  66. * @version wed dec 08 12:26:07 cst 2010
  67. * @param <array> $params 待初始化的参数
  68. */
  69. function init($params = array()) {
  70. if (count($params) > 0) {
  71. foreach ($params as $key => $val) {
  72. if (isset($this->$key)) {
  73. $this->$key = $val;
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * 创建分页链接
  80. *
  81. * @see create_links()
  82. * @author laurence.xu <haohailuo@163.com>
  83. * @version wed dec 08 15:02:27 cst 2010
  84. * @param <boolean> $show_info 是否显示总条数等信息
  85. * @return <string> $output
  86. */
  87. function create_links($show_info = false, $top_info = false) {
  88. //如果没有记录或者每页条数为0,则返回空
  89. if ($this->total_rows == 0 || $this->per_page == 0) {
  90. return '';
  91. }
  92. //计算总页数
  93. $num_pages = ceil($this->total_rows / $this->per_page);
  94. //只有一页,返回空
  95. if ($num_pages == 1 && !$show_info) {
  96. return '';
  97. }
  98. $ci =& get_instance();
  99. //获取当前页编号
  100. if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
  101. if ($ci->input->get($this->query_string_segment) != 0) {
  102. $this->cur_page = $ci->input->get($this->query_string_segment);
  103. // prep the current page - no funny business!
  104. $this->cur_page = (int) $this->cur_page;
  105. }
  106. } else {
  107. if (intval($this->custom_cur_page) > 0) {
  108. $this->cur_page = (int) $this->custom_cur_page;
  109. }else{
  110. $uri_segment = $ci->uri->segment($this->uri_segment, 0);
  111. if ( !emptyempty($uri_segment) ) {
  112. $this->cur_page = $uri_segment;
  113. //如果有下划线
  114. if ($this->underline_uri_seg >= 0) {
  115. if (strpos($this->cur_page, '-') !== false) {
  116. $arr = explode('-', $this->cur_page);
  117. }else {
  118. $arr = explode('_', $this->cur_page);
  119. }
  120. $this->cur_page = $arr[$this->underline_uri_seg];
  121. unset($arr);
  122. }
  123. // prep the current page - no funny business!
  124. $this->cur_page = (int) $this->cur_page;
  125. }
  126. }
  127. }
  128. //echo $this->cur_page;exit;
  129. //左右显示的页码个数
  130. $this->num_links = (int)$this->num_links;
  131. if ($this->num_links < 1) {
  132. show_error('your number of links must be a positive number.');
  133. }
  134. if ( ! is_numeric($this->cur_page) || $this->cur_page < 1) {
  135. $this->cur_page = 1;
  136. }
  137. //如果当前页数大于总页数,则赋值给当前页数最大值
  138. if ($this->cur_page > $num_pages) {
  139. $this->cur_page = $num_pages;
  140. }
  141. $uri_page_number = $this->cur_page;
  142. if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
  143. $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
  144. } else {
  145. $this->base_url = rtrim($this->base_url, '/') .'/';
  146. }
  147. if (strpos($this->base_url, "{page}") !== false) {
  148. $this->page_mode = 'replace';
  149. }
  150. $output = $top_output = '';
  151. //数据总量信息
  152. if ($show_info) {
  153. $output = " 共<b>".$this->total_rows ."</b>条记录 <span >{$this->cur_page}</span>/<b>".$num_pages."</b>页 每页<b>{$this->per_page}</b>条 ";
  154. }
  155. //数据信息,显示在上面,以供提醒
  156. if ($top_info) {
  157. $top_output = " 共 <b>".$this->total_rows ."</b> 条记录 第<span >{$this->cur_page}</span>页/共<b>".$num_pages."</b>页 ";
  158. }
  159. //判断是否要显示首页
  160. if ($this->cur_page > $this->num_links+1) {
  161. $output .= $this->first_tag_open.'<a href="'.$this->makelink().'">'.$this->first_link.'</a>'.$this->first_tag_close;
  162. }
  163. //显示上一页
  164. if ($this->cur_page != 1) {
  165. $j = $this->cur_page - 1;
  166. if ($j == 0) $j = '';
  167. $output .= $this->prev_tag_open.'<a href="'.$this->makelink($j).'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  168. }
  169. //显示中间页
  170. for ($i=1; $i <= $num_pages; $i++){
  171. if ($i < $this->cur_page-$this->num_links || $i > $this->cur_page+$this->num_links) {
  172. continue;
  173. }
  174. //显示中间页数
  175. if($this->cur_page == $i){
  176. $output .= $this->cur_tag_open.$i.$this->cur_tag_close; //当前页
  177. }else {
  178. $output .= $this->num_tag_open.'<a href="'.$this->makelink($i).'">'.$i.'</a>'.$this->num_tag_close;
  179. }
  180. }
  181. //显示下一页
  182. if ($this->cur_page < $num_pages) {
  183. $k = $this->cur_page + 1;
  184. $output .= $this->next_tag_open.'<a href="'.$this->makelink($k).'">'.$this->next_link.'</a>'.$this->next_tag_close;
  185. }
  186. //显示尾页
  187. if (($this->cur_page + $this->num_links) < $num_pages) {
  188. $output .= $this->last_tag_open.'<a href="'.$this->makelink($num_pages).'">'.$this->last_link.'</a>'.$this->last_tag_close;
  189. }
  190. $output = preg_replace("#([^:])//+#", "1/", $output);
  191. // add the wrapper html if exists
  192. $output = $this->full_tag_open.$output.$this->full_tag_close;
  193. if ($top_info) {
  194. return array($output, $top_output);
  195. }else {
  196. return $output;
  197. }
  198. }
  199. /**
  200. * 创建链接url地址
  201. *
  202. * @param <string> $str
  203. * @return <string>
  204. */
  205. function makelink($str = '') {
  206. if($this->page_mode == 'default') {
  207. return $this->_forsearch($this->base_url.$str);
  208. } else {
  209. $url = $this->base_url;
  210. if ($str == 1) {
  211. $url = str_replace('/{page}', '', $this->base_url);
  212. }
  213. $url = str_replace("{page}", $str, $url);
  214. return $this->_forsearch($url);
  215. }
  216. }
  217. /**
  218. * 处理url地址
  219. *
  220. * @see _forsearch()
  221. * @author laurence.xu <haohailuo@163.com>
  222. * @version wed dec 08 14:33:58 cst 2010
  223. * @param <string> $string pinfo
  224. * @return <string>
  225. */
  226. function _forsearch($string) {
  227. $length = strlen($string) - 1;
  228. if($string{$length} == '/') {
  229. $string = rtrim($string, '/');
  230. }
  231. return site_url($string);
  232. return $string;
  233. }
  234. }
  235. // end pagination class
  236. /* end of file hpages.php */
  237. /* location: ./system/libraries/hpages.php */