1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  
class Mypage_class {
    /**
    * @author :leanhunter<heww@live.com>
    * @create:2011-9-23
    * 本分页类专为三段式CI分页缩写,即site_url('control/function/2');
    */
      
     /**
      * config
      */
    public $part=2;//控制数字列表当前页前后链接数量
    public $totalpage=0;//总页数
    public $url='';//url地址,不含分页所在的段,形如:'control/function'
    public $total=0;//总条数
    public $perpage=5;//每页条数
    public $nowindex=1;//当前页
    public $seg=3;//页码参数取 index.php之后的段数,默认为3,即index.php/control/function/18 这种形式
    public $next_page='下一页';//下一页
    public $pre_page='上一页';//上一页
    public $first_page='首页';//首页
    public $last_page='末页';//尾页
     /**
      * constructor构造函数
      *
      * @param $params=array()
      */
     public function __construct($params array())
        {
            if (count($params) > 0)
            {
                $this->initialize($params);
            }
            log_message('debug'"MyPage Class Initialized");
        }
  
        function initialize($params)
        {
            if (count($params) > 0)
            {
                $this->total=isset($params['total']) ? intval($params['total']) : 0;//总条数
                $this->perpage=isset($params['perpage']) ? intval($params['perpage']) : 5;//每页条数
                $this->nowindex=isset($params['nowindex']) ? intval($params['nowindex']) : 1;//当前页
                $this->url=isset($params['url']) ? $params['url'] : '';//url地址,不含分页所在的段,形如:'control/function'
                $this->part=isset($params['part']) ? $params['part'] : 2;//控制数字列表当前页前后链接数量
                $this->seg=isset($params['seg']) ? $params['seg'] : 3;//页码参数取 index.php之后的段数,默认为3,即index.php/control/function/18 这种形式
                $this->next_page=isset($params['next_page']) ? $params['next_page'] : '下一页';
                $this->pre_page=isset($params['pre_page']) ? $params['pre_page'] : '上一页';
                $this->first_page=isset($params['first_page']) ? $params['first_page'] : '首页';
                $this->last_page=isset($params['last_page']) ? $params['last_page'] : '末页';
            }
            $this->totalpage=ceil$this->total / $this->perpage);//总页数
            $this->_myset_url($this->url);//设置链接地址
        }
  
     /**
      * 获取显示"下一页"的代码
      *
      * @param string $style
      * @return string
      */
     function next_page($style='pg_next')
     {
         if($this->nowindex < $this->totalpage){
            return $this->_get_link($this->_get_url($this->nowindex + 1), $this->next_page, $style);
         }
         return '<span class="'.$style.'">'.$this->next_page.'</span>';
     }
   
     /**
      * 获取显示“上一页”的代码
      *
      * @param string $style
      * @return string
      */
    function pre_page($style='pg_pre')
    {
        if($this->nowindex > 1){
            return $this->_get_link($this->_get_url($this->nowindex-1),$this->pre_page,$style);
        }
        return '<span class="'.$style.'">'.$this->pre_page.'</span>';
    }
   
     /**
      * 获取显示“首页”的代码
      *
      * @param string $style
      * @return string
      */
    function first_page($style='pg_first')
    {
        if($this->nowindex == 1){
            return '<span class="'.$style.'">'.$this->first_page.'</span>';
        }
        return $this->_get_link($this->_get_url(1),$this->first_page,$style);
    }
   
    /**
    * 获取显示“尾页”的代码
    *
    * @param string $style
    * @return string
    */
    function last_page($style='pg_last')
    {
        if($this->nowindex == $this->totalpage){
            return '<span class="'.$style.'">'.$this->last_page.'</span>';
        }
        return $this->_get_link($this->_get_url($this->totalpage),$this->last_page,$style);
    }
    /**
    * 获取显示“当前页”的代码
    *
    * @param string $style
    * @param string $nowindex_style
    * @return string
    */
    function nowbar($style='pg_link',$nowindex_style='pg_curr')
    {
        $plus=$this->part;
        $begin=1;
        $end=$this->totalpage;
          
        if ($this->nowindex > $plus) {
            $begin=$this->nowindex-$plus;
            $end $this->nowindex + $plus;
            if ($end $this->totalpage) {
                $begin= ($begin $end $this->totalpage>0) ? ($begin $end $this->totalpage) : 1;
                $end $this->totalpage;
            }
        else {
            $begin=1;
            $end $begin + 2*$plus;
            $end $end $this->totalpage ? $this->totalpage : $end;
        }
        $out='';
        for($i $begin;$i <= $end$i++)
        {
            if($i != $this->nowindex){
                $out.= $this->_get_link($this->_get_url($i),$i,$style);
            }else{
                $out.= '<span class="'.$nowindex_style.'">'.$i.'</span>';
            }
              
        }
          
        return $out;
    }
    /**
    * 获取显示跳转按钮的代码
    *
    * @return string
    */
    function select()
    {
        $out='<select name="pagelect" class="pg_select">';
        for($i=1;$i <= $this->totalpage;$i++)
        {
            if($i==$this->nowindex){
                $out.='<option value="'.$i.'" selected>'.$i.'</option>';
            }else{
                $out.='<option value="'.$i.'">'.$i.'</option>';
            }
        }
        $out.='</select>';
        return $out;
    }
    /**
    * 控制分页显示风格
    *
    * @param int $mode
    * @return string
    */
    function show($mode=1)
    {
        switch ($mode)
        {
            case 1://上一页 1 2 3 4 5 下一页 第x页
                return $this->pre_page().$this->nowbar().$this->next_page();
                break;
            case 2://首页 上一页 1 2 3 4 5 下一页  末页 第x页
                return $this->first_page().$this->pre_page().$this->nowbar().$this->next_page().$this->last_page().'&nbsp;第'.$this->select().'页';
                break;
            case 3://上一页 1 2 3 4 5 下一页
                return $this->pre_page().$this->nowbar().$this->next_page();
                break;
            default://上一页 1 2 3 4 5 下一页  第x页
                return $this->pre_page().$this->nowbar().$this->next_page().'&nbsp;第'.$this->select().'页';
                break;
        }
    }
/*----------------private function (私有方法)-----------------------------------------------------------*/
  
    /**
    * 设置url头地址
    * @param: String $url
    * @return boolean
    */
    public function _myset_url($url)
    {
        $CI=&get_instance();
        $CI->load->helper('url');
        if (empty($url)) {//如果$url为空,要用uri_string()函数取uri段
            $cururl='';
            $cururl=uri_string();
            $segementarray=explode("/",$cururl);
            $c=0;
            for ($i=0; $i < sizeof($segementarray); $i++) {
                if ($segementarray[$i] && $c $this->seg-1) {//取uri_string()的seg-1段
                    $url=$url.'/'.$segementarray[$i];
                    $c++;
                }
            }  
        }
        $this->url=site_url($url);
    }
      
    /**
    * 为指定的页面返回地址值
    *
    * @param int $pagenum
    * @return string $url
    */
    function _get_url($pagenum=1)
    {
        return $this->url.'/'.$pagenum;
    }
   
    /**
    * 获取链接地址
    */
    function _get_link($url,$text,$style=''){
        $style=$style?'class="'.$style.'"':'';
        return '<a '.$style.' href="'.$url.'">'.$text.'</a>';
    }
  
}
?>

把类下了放到library文件夹中,在需要分页的控制器中初始化,部分代码如下:

1
2
3
4
5
6
7
8
9
$page_config['perpage']=5;   //每页条数
$page_config['part']=2;//当前页前后链接数量
$page_config['url']='work/managework';//url
$page_config['seg']=3;//参数取 index.php之后的段数,默认为3,即index.php/control/function/18 这种形式
$page_config['nowindex']=$this->uri->segment($page_config['seg']) ? $this->uri->segment($page_config['seg']):1;//当前页
$this->load->library('mypage_class');
$countnum=$this->Work_mdl->count_work();//得到记录总数
$page_config['total']=$countnum['count(*)'];
$this->mypage_class->initialize($page_config);

视图里,在需要显示分页条的地方,添加:

1
2
3
<div class="page">
<?php echo $this->mypage_class->show(1); ?>
</div>

我自己山寨了一下淘宝的分页的css:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/******************** page **************************/
.page{
    font-size: 12px;
    height: 30px;
    text-align: right;
    padding-right:120px;
}
.pg_pre,.pg_next{
    border: 1px solid #ccc;
    margin:0 3px;
    background-image: url("../image/icon_pg.gif");
    background-repeat:no-repeat;
}
.pg_pre{
    padding: 3px 8px 3px 20px;
    background-position:7px 3px;
}
a.pg_pre{
    background-position:7px -19px;
}
.pg_next{
    padding: 3px 20px 3px 8px;
    background-position:-22px 3px;
}
a.pg_next{
    background-position:-22px -19px;
}
  
.pg_link{
    padding: 3px 8px;
    margin:0 3px;
    border: 1px solid #ccc;
}
.pg_link:hover,a.pg_next:hover,a.pg_pre:hover{
    border: 1px solid #A9C9E2;
    color: #137cce;
}
.pg_curr{
    padding: 3px 8px;
    margin:0 3px;
    color: #137cce;
    #E8F5FE;
    border: 1px solid #A9C9E2;
  
}
.pg_select{
    border:1px solid #ccc;
    padding:2px;
    margin:0 5px;
}

  css中用到的图片如下:

效果图示例:

***CI分页:为CodeIgniter写的分页类的更多相关文章

  1. 二十八、CI框架之自己写分页类,符合CI的路径规范

    一.参照了CSDN上某个前辈写的一个CI分页类,自己删删改改仿写了一个类似的分页类,代码如下: 二.我们在模型里面写2个数据查询的函数,一个用于查询数据数量,一个用于查询出具体数据 三.我们在控制器里 ...

  2. yii下多条件多表组合查询以及自写ajax分页

    多条件组合查询主要用到yii的CDbCriteria,这个类很多oem框架都有,非常好用. 前台表单 前台查询表单效果是这样的,多个条件组,每个组里放多个input,name为数组.当任何一个复选框被 ...

  3. 基于C#在Mongodb的Skip-Limit和Where-Limit的分页对比 并且含mongodb帮助类的源码

    最近在设计的日志服务中需要用到Mongodb这个Nosql数据库(不知道Mongodb的点我),由于是用于纯存日志,而且日志量巨大,百万千万级的,所以需要用到它的分页查询. 不过LZ也是刚刚接触这个数 ...

  4. MyBatis-编写自定义分页插件

    一.基础知识 本文测试和源码分析参考版本: Mybatis-version:3.5.5 本文相关测试源代码:https://github.com/wuya11/mybatis_demo 1.1 参考方 ...

  5. 用vue.js的v-for,v-if,computed写一个分页样式

    在学Vue,总想写个分页,先写了一个样式. 主要看思路: 思路简单,得到总页数,判断总页数,循环. 先判断总页数是否需要分页,总页数==1页就不分了. 再判断总页数<11就不用--. 总页数&g ...

  6. CI框架的引导流程以及基准测试类

    一[CI]框架的引导流程了解一下,并掌握如何新增自己的扩展类库   http://www.cnblogs.com/ohmygirl/p/CIRead-4.html // CI框架源码阅读笔记4 引导文 ...

  7. laravel基础课程---15、分页及验证码(lavarel分页效果如何实现)

    laravel基础课程---15.分页及验证码(lavarel分页效果如何实现) 一.总结 一句话总结: 数据库的paginate方法:$data=\DB::table("user" ...

  8. js 一个自写的 监测类

    自从认识了jQuery后,很多页面加载入口,都放在document.ready里面.但是有时候这个觉得ready加载太慢, 这个[监测类 ]就开始产生了 效果类似这个. 每10毫秒检查一次,直到加载了 ...

  9. Java基础-继承-编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数 loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个 类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功 能。

    #29.编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数 wheels和车重weight.小车类Car是Vehicle的子类,其中包含的属性有载人数 loader.卡车类T ...

随机推荐

  1. .NET清除Session 的几个方法[clear/removeAll/remove/Abandon]

    1.clear() 清空所有session对象的值,但保留会话   2.removeAll() 调用clear()方法   3.remove("SessionName") 删除某个 ...

  2. Rhythmbox中文乱码解决办法

    今天在网络上找到了一个比较好的解决Rhythmbox中文乱码的问题的方法 进入你的音乐文件夹执行如下代码: mid3iconv -e GBK *.mp3 如果没有提示多试几次,有可能系统会提示: py ...

  3. 基于 unity ngui 上的滚动加载__UiVirtual

    在游戏里面经常会有背包,好友,对话,这样的列表.当列表的内容多了,如果一打开界面就对所有内容进行实例化,会消耗大量的性能,且会造成UI上的卡顿. 于是便需要,在列表里面只实例化屏幕上可见的item.屏 ...

  4. C++二维数组动态内存分配

    对于二维数组和二维指针的内存的分配 这里首选说一下一维指针和一维数组的内存分配情况. 一维: 数组:形如int  a[5];这里定义了一个一维数组a,并且数组的元素个数是5,这里的a是这五个元素的整体 ...

  5. Zencart 国家排序及中文名称的扩展

    最终实现效果如上 具体步骤: 1. 手动或SQL修改数据表,增加2个字段 ) ) '; 2. 修改admin/countries.php文件,增加表单插入编辑功能, 共计7处,此处忽略具体代码. 3. ...

  6. php 读取文件头判断文件类型的实现代码

    php代码实现读取文件头判断文件类型,支持图片.rar.exe等后缀. 例子: <?php $filename = "11.jpg"; //为图片的路径可以用d:/uploa ...

  7. (转)汉字转拼音HanziToPinyin

    本文转载于:http://blog.csdn.net/zhangphil/article/details/47164665 Android系统本身自带有有将汉字转化为英文拼音的类和方法.具体的类就是H ...

  8. linux前景到底怎么样啊?

    我就不长篇大论,举四个例子你看看. 1.目下最热最潮最流行的云计算技术的背后是虚拟化和网格技术,而虚拟化和网格技术基本是Linux的天下,目前虚拟化的三大家:Vmware,Xen,Hyper-V中,市 ...

  9. js实现复制到剪切板

    // <![CDATA[ function copy_clip(copy) { if (window.clipboardData) { window.clipboardData.setData( ...

  10. Log Parser 2.2

    Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 A ...