-
php codeigniter框架分页类
codeigniter 具有非常容易使用的分页类,在本教程中我会做一个从数据库教程返回一组结果并分页这些结果的简单例子,我将使用最新版本的 ci,分页类并没有修改,至少我认为没有,用最新的稳定版框架总是好的.
调用方法,代码如下:
- //创建分页
- $config = array();
- $this->load->library('hpages');
- $config['base_url'] = "channel/lists/c{$slug}/{page}";
- $config['total_rows'] = intval($total);
- $config['per_page'] = $pagesize;
- //开源代码phpfensi.com
- $config['uri_segment'] = 1;
- $config['num_links'] = 3;
- $config['underline_uri_seg'] = 1; //下划线uri中页数所在的位置
- $this->hpages->init($config);
- $this->template['lists'] = $list;
- $this->template['pagestr'] = $this->hpages->create_links(1);
php文件代码,实例如下:
- <?php if (! defined('basepath')) exit('access denied!');
- /**
- * file_name : hpages.php
- * 浩海网络 前台 分页类
- *
- * @package haohailuo
- * @author by laurence.xu <haohailuo@163.com>
- * @copyright copyright (c) 2010, haohailuo, inc.
- * @link http://www.haohailuo.com
- * @since version 1.0 $id$
- * @version wed dec 08 12:21:17 cst 2010
- * @filesource
- */
- class hpages {
- var $base_url = ''; //基本链接地址
- var $total_rows = ''; //数据总数
- var $per_page = 10; //每页条数
- var $num_links = 2; //要显示的左右链接的个数
- var $cur_page = 1; //当前页数
- var $first_link = '‹ first'; //首页字符
- var $next_link = '>'; //下一页的字符
- var $prev_link = '<'; //上一页的字符
- var $last_link = 'last ›'; //末页的字符
- var $uri_segment = 3; //分页数所在的uri片段位置
- var $full_tag_open = ''; //分页区域开始的html标签
- var $full_tag_close = ''; //分页区域结束的后html标签
- var $first_tag_open = ''; //首页开始的html标签
- var $first_tag_close = ' '; //首页结束的html标签
- var $last_tag_open = ' '; //末页开始的html标签
- var $last_tag_close = ''; //末页结束的html标签
- var $cur_tag_open = ' <b>';//当前页开始的...
- var $cur_tag_close = '</b>'; //当前页结束的...
- var $next_tag_open = ' '; //下一页开始的.....
- var $next_tag_close = ' '; //下一页结束的.....
- var $prev_tag_open = ' '; //上一页开始的.....
- var $prev_tag_close = ''; //上一页结束的.....
- var $num_tag_open = ' '; //“数字”链接的打开标签。
- var $num_tag_close = ''; //“数字”链接的结束标签。
- var $page_query_string = false;
- var $query_string_segment = 'per_page';
- var $page_mode = 'default'; //default for add page at the end? if include {page}, will replace it for current page.
- var $underline_uri_seg = -1; //存在下划线时,页码所在数组下标位置
- var $custom_cur_page = 0; //自定义当前页码,存在此值是,系统将不自动判断当前页数,默认不启用
- function __construct() {
- $this->hpages();
- }
- /**
- * constructor
- *
- * @access public
- */
- function hpages() {
- if (file_exists(apppath.'config/pagination.php')) {
- require_once(apppath.'config/pagination.php');
- foreach ($config as $key=>$val) {
- $this->{$key} = $val;
- }
- }
- log_message('debug', "hpages class initialized");
- }
- /**
- * 初始化参数
- *
- * @see init()
- * @author laurence.xu <haohailuo@163.com>
- * @version wed dec 08 12:26:07 cst 2010
- * @param <array> $params 待初始化的参数
- */
- function init($params = array()) {
- if (count($params) > 0) {
- foreach ($params as $key => $val) {
- if (isset($this->$key)) {
- $this->$key = $val;
- }
- }
- }
- }
- /**
- * 创建分页链接
- *
- * @see create_links()
- * @author laurence.xu <haohailuo@163.com>
- * @version wed dec 08 15:02:27 cst 2010
- * @param <boolean> $show_info 是否显示总条数等信息
- * @return <string> $output
- */
- function create_links($show_info = false, $top_info = false) {
- //如果没有记录或者每页条数为0,则返回空
- if ($this->total_rows == 0 || $this->per_page == 0) {
- return '';
- }
- //计算总页数
- $num_pages = ceil($this->total_rows / $this->per_page);
- //只有一页,返回空
- if ($num_pages == 1 && !$show_info) {
- return '';
- }
- $ci =& get_instance();
- //获取当前页编号
- if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
- if ($ci->input->get($this->query_string_segment) != 0) {
- $this->cur_page = $ci->input->get($this->query_string_segment);
- // prep the current page - no funny business!
- $this->cur_page = (int) $this->cur_page;
- }
- } else {
- if (intval($this->custom_cur_page) > 0) {
- $this->cur_page = (int) $this->custom_cur_page;
- }else{
- $uri_segment = $ci->uri->segment($this->uri_segment, 0);
- if ( !emptyempty($uri_segment) ) {
- $this->cur_page = $uri_segment;
- //如果有下划线
- if ($this->underline_uri_seg >= 0) {
- if (strpos($this->cur_page, '-') !== false) {
- $arr = explode('-', $this->cur_page);
- }else {
- $arr = explode('_', $this->cur_page);
- }
- $this->cur_page = $arr[$this->underline_uri_seg];
- unset($arr);
- }
- // prep the current page - no funny business!
- $this->cur_page = (int) $this->cur_page;
- }
- }
- }
- //echo $this->cur_page;exit;
- //左右显示的页码个数
- $this->num_links = (int)$this->num_links;
- if ($this->num_links < 1) {
- show_error('your number of links must be a positive number.');
- }
- if ( ! is_numeric($this->cur_page) || $this->cur_page < 1) {
- $this->cur_page = 1;
- }
- //如果当前页数大于总页数,则赋值给当前页数最大值
- if ($this->cur_page > $num_pages) {
- $this->cur_page = $num_pages;
- }
- $uri_page_number = $this->cur_page;
- if ($ci->config->item('enable_query_strings') === true || $this->page_query_string === true) {
- $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'=';
- } else {
- $this->base_url = rtrim($this->base_url, '/') .'/';
- }
- if (strpos($this->base_url, "{page}") !== false) {
- $this->page_mode = 'replace';
- }
- $output = $top_output = '';
- //数据总量信息
- if ($show_info) {
- $output = " 共<b>".$this->total_rows ."</b>条记录 <span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>/<b>".$num_pages."</b>页 每页<b>{$this->per_page}</b>条 ";
- }
- //数据信息,显示在上面,以供提醒
- if ($top_info) {
- $top_output = " 共 <b>".$this->total_rows ."</b> 条记录 第<span style='color:#ff0000;font-weight:bold'>{$this->cur_page}</span>页/共<b>".$num_pages."</b>页 ";
- }
- //判断是否要显示首页
- if ($this->cur_page > $this->num_links+1) {
- $output .= $this->first_tag_open.'<a href="'.$this->makelink().'">'.$this->first_link.'</a>'.$this->first_tag_close;
- }
- //显示上一页
- if ($this->cur_page != 1) {
- $j = $this->cur_page - 1;
- if ($j == 0) $j = '';
- $output .= $this->prev_tag_open.'<a href="'.$this->makelink($j).'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
- }
- //显示中间页
- for ($i=1; $i <= $num_pages; $i++){
- if ($i < $this->cur_page-$this->num_links || $i > $this->cur_page+$this->num_links) {
- continue;
- }
- //显示中间页数
- if($this->cur_page == $i){
- $output .= $this->cur_tag_open.$i.$this->cur_tag_close; //当前页
- }else {
- $output .= $this->num_tag_open.'<a href="'.$this->makelink($i).'">'.$i.'</a>'.$this->num_tag_close;
- }
- }
- //显示下一页
- if ($this->cur_page < $num_pages) {
- $k = $this->cur_page + 1;
- $output .= $this->next_tag_open.'<a href="'.$this->makelink($k).'">'.$this->next_link.'</a>'.$this->next_tag_close;
- }
- //显示尾页
- if (($this->cur_page + $this->num_links) < $num_pages) {
- $output .= $this->last_tag_open.'<a href="'.$this->makelink($num_pages).'">'.$this->last_link.'</a>'.$this->last_tag_close;
- }
- $output = preg_replace("#([^:])//+#", "1/", $output);
- // add the wrapper html if exists
- $output = $this->full_tag_open.$output.$this->full_tag_close;
- if ($top_info) {
- return array($output, $top_output);
- }else {
- return $output;
- }
- }
- /**
- * 创建链接url地址
- *
- * @param <string> $str
- * @return <string>
- */
- function makelink($str = '') {
- if($this->page_mode == 'default') {
- return $this->_forsearch($this->base_url.$str);
- } else {
- $url = $this->base_url;
- if ($str == 1) {
- $url = str_replace('/{page}', '', $this->base_url);
- }
- $url = str_replace("{page}", $str, $url);
- return $this->_forsearch($url);
- }
- }
- /**
- * 处理url地址
- *
- * @see _forsearch()
- * @author laurence.xu <haohailuo@163.com>
- * @version wed dec 08 14:33:58 cst 2010
- * @param <string> $string pinfo
- * @return <string>
- */
- function _forsearch($string) {
- $length = strlen($string) - 1;
- if($string{$length} == '/') {
- $string = rtrim($string, '/');
- }
- return site_url($string);
- return $string;
- }
- }
- // end pagination class
- /* end of file hpages.php */
- /* location: ./system/libraries/hpages.php */
出处:http://www.phpfensi.com/php/20140908/5062.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式