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 相关文章推荐
无法载入 mcrypt 扩展,请检查 PHP 配置终极解决方案
Jul 18 PHP
PHP获取网址的顶级域名函数代码
Sep 24 PHP
php数组的概述及分类与声明代码演示
Feb 26 PHP
PHP设计模式之责任链模式的深入解析
Jun 13 PHP
php加密解密实用类分享
Jan 07 PHP
php过滤所有恶意字符(批量过滤post,get敏感数据)
Mar 18 PHP
php生成静态页面的简单示例
Apr 17 PHP
ThinkPHP应用模式扩展详解
Jul 16 PHP
PHP加密解密实例分析
Dec 25 PHP
php 调用ffmpeg获取视频信息的简单实现
Apr 03 PHP
PHP设计模式之工厂模式(Factory Pattern)的讲解
Mar 21 PHP
实例讲解PHP表单
Jun 10 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
一道关于php变量引用的面试题
2010/08/08 PHP
javascript,php获取函数参数对象的代码
2011/02/03 PHP
php正则表达式基本知识与应用详解【经典教程】
2017/04/17 PHP
laravel admin实现分类树/模型树的示例代码
2020/06/10 PHP
基于Jquery插件开发之图片放大镜效果(仿淘宝)
2011/11/19 Javascript
JS 实现Table相同行的单元格自动合并示例代码
2013/08/27 Javascript
jQuery插件slicebox实现3D动画图片轮播切换特效
2015/04/12 Javascript
Bootstrap模块dropdown实现下拉框响应
2016/05/22 Javascript
canvas雪花效果核心代码分享
2017/02/19 Javascript
jQuery使用JSONP实现跨域获取数据的三种方法详解
2017/05/04 jQuery
JS作用域链详解
2017/06/26 Javascript
js图片上传的封装代码
2017/08/01 Javascript
详谈表单重复提交的三种情况及解决方法
2017/08/16 Javascript
JavaScript实现离开页面前提示功能【附jQuery实现方法】
2017/09/26 jQuery
js导出Excel表格超出26位英文字符的解决方法ES6
2017/11/15 Javascript
elementUI Tree 树形控件的官方使用文档
2019/04/25 Javascript
[01:06]DOTA2小知识课堂 Ep.02 吹风竟可解梦境缠绕
2019/12/05 DOTA
详细讲解用Python发送SMTP邮件的教程
2015/04/29 Python
Python 模拟登陆的两种实现方法
2017/08/10 Python
Python实现正整数分解质因数操作示例
2018/08/01 Python
python3调用windows dos命令的例子
2019/08/14 Python
python与mysql数据库交互的实现
2020/01/06 Python
python代码实现TSNE降维数据可视化教程
2020/02/28 Python
Python实现发票自动校核微信机器人的方法
2020/05/22 Python
解决Keras使用GPU资源耗尽的问题
2020/06/22 Python
NFL墨西哥官方商店:Tienda NFL
2017/11/28 全球购物
RIP版本1跟版本2的区别
2013/12/30 面试题
如何清空Session
2015/02/23 面试题
法学专业个人求职信
2013/09/26 职场文书
心得体会的写法
2014/09/05 职场文书
党员对照检查剖析材料
2014/10/13 职场文书
信访维稳承诺书
2015/05/04 职场文书
个人欠条范本
2015/07/03 职场文书
导游词之阳朔遇龙河
2019/12/16 职场文书
为什么MySQL 删除表数据 磁盘空间还一直被占用
2021/10/16 MySQL
Mysql索引失效 数据库表中有索引还是查询很慢
2022/05/15 MySQL