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

   2015-10-28 0
核心提示:这篇文章主要介绍了codeigniter中测试通过的分页类示例,需要的朋友可以参考下

通用分页类(以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>

 
标签: codeigniter 分页
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • Codeigniter中禁止A Database Error Occurred及
    页面出现如下两种错误提示:A PHP Error was encounteredSeverity: NoticeMessage: Trying to get property of non-objectFilename: XXXXXLine Number: 67A Database Error OccurredError Number: 1064You have an error in your SQL syntax; check the manua
    02-09
  • CodeIgniter 3.1.3 发布,PHP 的 MVC 框架
    CodeIgniter 3.1.3 发布了,CodeIgniter 是一个简单快速的 PHP MVC 框架。该版本带来了一些关键的安全性修复,以及许多的错误修复。安全:修复了电子邮件的处理问题,XSS 漏洞以及一些 CSRF 强化 Bug 修复:针对数据库,电子邮件,文件上传,图像处理,输入,
  • 管理你的应用程序
    管理你的应用程序http://codeigniter.org.cn/user_guide/general/managing_apps.html默认情况下,CodeIgniter 假设你只有一个应用程序,被放置在application/目录下。但是,你完全可以拥有多个程序并让 它们共享一份 CodeIgniter 。你甚至也可以对你的应用程
  • CodeIgniter源码阅读笔记
    入口文件index.php中主要定义了一些全局路径变量如BASEPATH和APPPATH这种常用的变量并且可以配置代码的部署环境,最后require真正的核心文件CodeIgniter.phpCodeIgniter.php位于system/core/目录下,该文件是最主要的核心文件,他负责引入全局需要用到的一些
  • CodeIgniter 3.1.1 发布,PHP 的 MVC 框架
    CodeIgniter 3.1.1 发布了,CodeIgniter 是一个简单快速的PHP MVC 框架。改变: Added E_PARSE to the list of error levels detected by the shutdown handler.BUG修复: Fixed a bug (#4732) - :doc:`Session Library libraries/sessions` triggered errors
  • CodeIgniter 3.1.2 发布,PHP 的 MVC 框架
    CodeIgniter 3.1.2 发布了,CodeIgniter 是一个简单快速的PHP MVC 框架。改进内容:改变:SecurityFixed a number of new vulnerabilities in :doc:`Security Library libraries/security` method xss_clean() . General ChangesAllowed PHP 4-style construc
  • CodeIgniter 4 建议路线图
    CodeIgniter 4 建议路线图
    作者: Lonnie Ezell (CI 理事会成员)翻译: Hex 原文:CodeIgniter 4 Proposed Roadmap原文发表于 2015 年 8 月 5 日 ,截止目前 CI 4 正在开发中,尚未发布 ******************* 分割线 *******************我们综合考虑了社区的愿望和意见后,也对什么样的未
    10-31 CodeIgniter
  • Codeigniter ACL library
    以下是代码片段:以下是代码片段:?phpif(!defined('BASEPATH'))exit('No direct script access allowed');/*** MX_ACL - Access Control Library PHP5** Notes:* $config['cache_path'] must be set** Install this file as application/libraries/MX_ACL.ph
    10-31 CodeIgniter
  • CodeIgniter生成静态页的方法
    本文实例讲述了CodeIgniter生成静态页的方法。分享给大家供大家参考,具体如下:现在我们来开发如何让CI框架生成静态页面.下面直接帖代码:$this-output-get_output();使用这个方法,你可以可以得到将要输出的数据,并把它保存起来,留着它用(我们做新闻类型
    09-08 CodeIgniter
  • 翻译:CodeIgniter框架在PHPStorm中实现自动完
    在使用CI框架的过程中,因为调用的方式不是原生的PHP方式是在CodeIgniter的基础上进行开发。没有自动完成功能,这对编程来说是非常不方便的。如代码:$this-load-view('index'); 这时候我们把鼠标移动到load上并没有智能提示和查看定义的功能。为了解决这个问
点击排行