codeigniter中测试通过的分页类示例


Posted in PHP onApril 17, 2014

通用分页类(以Codeigniter测试)

codeigniter中测试通过的分页类示例

page_list.php

<?php if( ! defined('BASEPATH')) die('No Access');
/**
 * 分页类
 */
class Page_list {
    /**
     * 总数据
     * @var int
     */
    private $total;
    /**
     * 每页显示数据
     * @var int
     */
    private $size;
    /**
     * 当前页数
     * @var int
     */
    private $page;
    /**
     * 页数列表左右页数
     * @var int
     */
    private $len;
    /**
     * 总页数
     * @var int
     */
    private $page_total;
    /**
     * 页码列表
     * @var array
     */
    private $page_list;
    /**
     * 基准地址
     * @var string
     */
    private $base_url;
    /**
     * 替换标志
     * @var string
     */
    private $place;
    /**
     * 分页样式
     * @var string
     */
    private $style;

    /**
     * 构造函数
     *
     * @param array $config 配置数组
     */
    public function __construct($config = array()){
        // 初始化默认值
        $this->total = 0;
        $this->size = 20;
        $this->page = 1;
        $this->len = 4;
        $this->page_total = 1;
        $this->page_list = array();
        $this->base_url = '?page=-page-';
        $this->place = '-page-';
        $this->style = $this->get_default_style();
        $this->initialize($config);
    }
    /**
     * 初始化分页
     *
     * @param array $config 配置数组
     */
    public function initialize($config = array()){
        // 取得配置值
        if(is_array($config)){
            if(array_key_exists('total', $config)) $this->total = @intval($config['total']);
            if(array_key_exists('size', $config)) $this->size = @intval($config['size']);
            if(array_key_exists('page', $config)) $this->page = @intval($config['page']);
            if(array_key_exists('len', $config)) $this->len = @intval($config['len']);
            if(array_key_exists('base_url', $config)) $this->base_url = @strval($config['base_url']);
            if(array_key_exists('place', $config)) $this->place = @strval($config['place']);
            if(array_key_exists('style', $config)) $this->style = @strval($config['style']);
        }
        // 修正值
        if($this->total<0) $this->total = 0;
        if($this->size<=0) $this->size = 20;
        if($this->page<=0) $this->page = 1;
        if($this->len<=0) $this->len = 4;
        // 执行分页算法
        $this->page_total = ceil($this->total/$this->size);
        if($this->page_total<=0) $this->page_total = 1;
        if($this->page>$this->page_total) $this->page = $this->page_total;
        if($this->page-$this->len>=1){
            for($i=$this->len; $i>0; $i--){
                $this->page_list[] = $this->page - $i;
            }
        }else{
            for($i=1; $i<$this->page; $i++){
                $this->page_list[] = $i;
            }
        }
        $this->page_list[] = $this->page;
        if($this->page+$this->len<=$this->page_total){
            for($i=1; $i<=$this->len; $i++){
                $this->page_list[] = $this->page + $i;
            }
        }else{
            for($i=$this->page+1; $i<=$this->page_total; $i++){
                $this->page_list[] = $i;
            }
        }
    }
    /**
     * 默认分页样式
     *
     * @return string
     */
    public function get_default_style(){
        $style = '<style type="text/css">';
        $style .= ' div.page_list { margin:0;padding:0;overflow:hidden;zoom:1;}';
        $style .= ' div.page_list a {display:block;float:left;height:20px;line-height:21px; font-size:13px;font-weight:normal;font-style:normal;color:#133DB6;text-decoration:none;margin:0 3px;padding:0 7px;overflow;zoom:1;}';
        $style .= ' div.page_list a.page_list_act { font-size:13px;padding:0 6px;}';
        $style .= ' div.page_list a:link, div.page_list a:visited { background:#FFF;border:1px #EEE solid;text-decoration:none;}';
        $style .= ' div.page_list a:hover, div.page_list a:active { background:#EEE;text-decoration:none;}';
        $style .= ' div.page_list strong { display:block;float:left;height:20px;line-height:21px;font-size:13px;font-weight:bold;font-style:normal;color:#000;margin:0 3px;padding:0 8px;overflow:hidden;zoom:1;}';
        $style .= ' </style>';
        return $style;
    }
    /**
     * 是否是第一页
     *
     * @return bool
     */
    public function is_first_page(){
        return $this->page == 1;
    }
    /**
     * 获取第一页页码
     *
     * @return int
     */
    public function get_first_page(){
        return 1;
    }
    /**
     * 是否是最后一页
     *
     * @return bool
     */
    public function is_last_page(){
        return $this->page == $this->page_total;
    }
    /**
     * 获取最后一页页码
     *
     * @return int
     */
    public function get_last_page(){
        return $this->page_total;
    }
    /**
     * 是否存在上一页
     *
     * @return bool
     */
    public function has_prev_page(){
        return $this->page > 1;
    }
    /**
     * 是否存在下一页
     *
     * @return bool
     */
    public function has_next_page(){
        return $this->page < $this->page_total;
    }
    /**
     * 获取上一页页码
     *
     * @return int
     */
    public function get_prev_page(){
        return $this->has_prev_page() ? $this->page - 1 : $this->page;
    }
    /**
     * 获取下一页页码
     *
     * @return int
     */
    public function get_next_page(){
        return $this->has_next_page() ? $this->page + 1 : $this->page;
    }
    /**
     * 获取当前页页码
     *
     * @return int
     */
    public function get_curr_page(){
        return $this->page;
    }
    /**
     * 获取总数据数
     *
     * @return int
     */
    public function get_total(){
        return $this->total;
    }
    /**
     * 获取每页显示数据数
     *
     * @return int
     */
    public function get_size(){
        return $this->size;
    }
    /**
     * 获取总页数
     *
     * @return int
     */
    public function get_total_page(){
        return $this->page_total;
    }
    /**
     * 构建并返回分页代码
     *
     * @param string $base_url 基准地址
     * @param string $place 替换标志
     * @param string $style 分页样式
     * @return string 分页HTML代码
     */
    public function display($base_url = '', $place = '', $style = ''){
        if($base_url==='') $base_url = $this->base_url;
        if($place==='') $place = $this->place;
        if($style==='') $style = $this->style;
        $str = $style.'<div class="page_list">';
        if( ! $this->is_first_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_first_page(), $base_url).'">首页</a>';
        }
        if($this->has_prev_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_prev_page(), $base_url).'">上一页</a>';
        }
        foreach($this->page_list as $v){
            if($v==$this->page){
                $str .= '<strong>' . $v . '</strong>';
            }else{
                $str .= '<a href="'.str_replace($place, $v, $base_url).'">'.$v.'</a>';
            }
        }
        if($this->has_next_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_next_page(), $base_url).'">下一页</a>';
        }
        if( ! $this->is_last_page()){
            $str .= '<a class="page_list_act" href="'.str_replace($place, $this->get_last_page(), $base_url).'">尾页</a>';
        }
        $str .= '</div>';
        return $str;
    }
}
?>

/application/view/pagelist.php

<?php if( ! defined('BASEPATH')) die('No Access');
    class Pagelist extends CI_Controller {
        public function page(){
            $this->load->helper('url');
            $page = $this->input->get('page');
            $page = @intval($page);
            if($page<=0) $page = 1;
            $this->load->library('page_list',array('total'=>10000,'size'=>16,'page'=>$page));
            $pl = $this->page_list->display(site_url('pagelist/page/page/-page-'));
            $this->load->view('pagelist', array('pl' => $pl));
        }
    }
?>

/application/view/pagelist.php

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    <title>分页测试</title>
</head>
<body>
<?php echo $pl; ?>
</body>
</html>
PHP 相关文章推荐
PHP字符编码问题之GB2312 VS UTF-8解决方法
Jun 23 PHP
php程序的国际化实现方法(利用gettext)
Aug 14 PHP
PHP 面向对象详解
Sep 13 PHP
php读取mssql的ntext字段返回值为空的解决方法
Dec 30 PHP
实现PHP框架系列文章(6)mysql数据库方法
Mar 04 PHP
图文详解PHP环境搭建教程
Jul 16 PHP
PHP入门教程之操作符与控制结构流程详解
Sep 09 PHP
DEDE实现转跳属性文档在模板上调用出转跳地址
Nov 04 PHP
PHP面向对象程序设计方法实例详解
Dec 24 PHP
Yii2汉字转拼音类的实例代码
Apr 18 PHP
php使用flock阻塞写入文件和非阻塞写入文件的实例讲解
Jul 10 PHP
PHP使Laravel为JSON REST API返回自定义错误的问题
Oct 16 PHP
php生成静态页面的简单示例
Apr 17 #PHP
php文件服务实现虚拟挂载其他目录示例
Apr 17 #PHP
php实现12306余票查询、价格查询示例
Apr 17 #PHP
PHP5.5在windows安装使用memcached服务端的方法
Apr 16 #PHP
纯PHP生成的一个树叶图片画图例子
Apr 16 #PHP
通过dbi使用perl连接mysql数据库的方法
Apr 16 #PHP
php sybase_fetch_array使用方法
Apr 15 #PHP
You might like
解析coreseek for sphinx的使用
2013/06/21 PHP
PHP导出Excel实例讲解
2016/01/24 PHP
Avengerls vs Newbee BO3 第二场2.18
2021/03/10 DOTA
jQuery TextBox自动完成条
2009/07/22 Javascript
JS清除IE浏览器缓存的方法
2013/07/26 Javascript
JS 获取select(多选下拉)中所选值的示例代码
2013/08/02 Javascript
jQuery实现将页面上HTML标签换成另外标签的方法
2015/06/09 Javascript
JavaScript数组对象实现增加一个返回随机元素的方法
2015/07/27 Javascript
浅析node连接数据库(express+mysql)
2015/11/30 Javascript
JQuery ztree 异步加载实例讲解
2016/02/25 Javascript
JS函数的定义与调用方法推荐
2016/05/12 Javascript
Knockoutjs 学习系列(一)ko初体验
2016/06/07 Javascript
Javascript打印局部页面实例
2016/06/21 Javascript
jquery实现垂直和水平菜单导航栏
2020/08/27 Javascript
jfinal与bootstrap的登出实战详解
2017/11/27 Javascript
Vue中使用 setTimeout() setInterval()函数的问题
2018/09/13 Javascript
基于javascript的无缝滚动动画实现2
2020/08/07 Javascript
jQuery+ajax实现用户登录验证
2020/09/13 jQuery
python中sets模块的用法实例
2014/09/30 Python
tensorflow识别自己手写数字
2018/03/14 Python
Python使用OpenCV进行标定
2018/05/08 Python
Python学习笔记之抓取某只基金历史净值数据实战案例
2019/06/03 Python
python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】
2019/06/17 Python
python flask几分钟实现web服务的例子
2019/07/26 Python
Python项目跨域问题解决方案
2020/06/22 Python
Python读写Excel表格的方法
2021/03/02 Python
大学生求职简历的自我评价
2013/10/21 职场文书
函授自我鉴定
2013/11/06 职场文书
购房协议书
2014/04/11 职场文书
调研汇报材料范文
2014/08/17 职场文书
2014年全国爱牙日宣传活动方案
2014/09/21 职场文书
学生违反校规检讨书
2014/10/28 职场文书
西游记读书笔记
2015/06/25 职场文书
队名及霸气口号大全
2015/12/25 职场文书
浅谈Redis在直播场景的实践方案
2021/04/27 Redis
一行Python命令实现批量加水印
2022/04/07 Python