用户
 找回密码
 入住 CI 中国社区
搜索
查看: 6883|回复: 11
收起左侧

[已解决] 稍微总结下分页用法

[复制链接]
发表于 2010-7-26 15:20:54 | 显示全部楼层 |阅读模式
本帖最后由 zmm1030 于 2010-7-27 17:14 编辑

$this->load->library('pagination');//实例化分页类

$config['base_url'] = 'http://example.com/index.php/text/page';页面链接(/text/page这两个参数其实跟分页没一点关系,其实就是控制层的类名和函数明)
这里为什么要用page这函数名?严重的误导。我还以为一用要用page,不过我接触CI也不深,虽然知道获取get方式有些不一样,大概我比较笨吧。教程里也有说明,我没仔细看。


这里补充一点,当$config['per_page'] < $config['total_rows']分页不显示(不是很友好)
源码:
PHP复制代码
// If our item count or per-page total is zero there is no need to continue.
                if ($this->total_rows == 0 OR $this->per_page == 0)
                {
                        return '';
                }
 
                // Calculate the total number of pages
                $num_pages = ceil($this->total_rows / $this->per_page);
                               
                // Is there only one page? Hm... nothing more to do here then.
                if ($num_pages == 1)
                {
                        return '';
                }
复制代码


$config['total_rows'] = 200;//总记录条数
$config['per_page'] = '20'; //跳码,经过这样设置后,用$this->uri->segment(5)获取的值=当前页码*20,我没搞懂,用下面sql解释下
源码注释:Max number of items you want shown per page:应该是说每页显示的最大记录条数书。

跳的是记录条数,也就是获取的动态页码,跟显示出来的页码没关系。比如现在的参数,写成SQL语句就是:$this->uri->segment(5)获取当前页码。
'select * from table LIMIT '.$this->uri->segment(5).',20'
--->'select * from table LIMIT 0, 20' 第一页
--->'select * from table LIMIT 20, 20' 第二页
--->'select * from table LIMIT 40, 20' 第二页
也就是说写的查询语句中LIMIT 应该显示条数与$config['per_page'] 一致,应该这么写
--->'select * from table LIMIT '.$this->uri->segment(5).', '.$config['per_page'];


$config['cur_page'] = $this->uri->segment(5);//这里是获取动态页码,官方居然漏写,不看源码根本就不知道,还要传这个参数。这点很重要。
源码注释:The current page being viewed(从当前页开始)


$this->pagination->initialize($config); //分页属性赋值

echo $this->pagination->create_links();//主函数调用

如上参数显示结果是« First  < 1 2 3 4 5 >  Last »(共10页)
链接是http://localhost/bus/index.php/
http://localhost/bus/index.php/text/page/20
http://localhost/bus/index.php/text/page/40
http://localhost/bus/index.php/text/page/60
http://localhost/bus/index.php/text/page/80
.
.
.
.
我的总结是官方的这个教程需要加一个参数,请尝试调试
$this->load->library('pagination');

$config['base_url'] = 'http://example.com/index.php/test/page/';
$config['total_rows'] = '200';
$config['per_page'] = '20';
$config['cur_page'] = $this->uri->segment(5);
$this->pagination->initialize($config);

echo $this->pagination->create_links();


以下是管理员给的教程:
--------------------------------------------------------
参考我翻译的分页教程
http://codeigniter.org.cn/forums/thread-17-1-1.html
http://codeigniter.org.cn/forums ... p;fromuid=2#pid5478
还有建议你看看 CI 教程索引,这里你需要的都有:
http://codeigniter.org.cn/forums/thread-214-1-1.html
发表于 2010-7-26 17:31:01 | 显示全部楼层
切换页面的时候,你没有向分页类传递你这个是第几页,所以,当前页面不加粗!
page 其实就是total_rows/per_page的值,至于只显示成1 2 3 > LAST >应该是分页类配置问题~
个人认为,没有去实际看看~
发表于 2010-7-26 17:50:06 | 显示全部楼层
本人测试成功~
cisystem library 下的pagination.php中        
var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
是设置前后间,显示多少个页面链接的,默认值是2 效果如1 2 3 >  Last ›  1是最前,LAST>是最后,2 3在中间,如果是3 则 1 2 3 4> last>
至于分页代码,你应该写在分页控制器的方法下面,如果你控制器是index 方法是page,那你page方法里面就要包含分页类的代码
$config['base_url'] =  site_url(/index/page/'); 应该这样设置
 楼主| 发表于 2010-7-26 18:03:25 | 显示全部楼层
回复 3# hj_545
var $cur_page=  0; // The current page being viewed
这个属性是不是就是动态页码~
发表于 2010-7-26 18:04:44 | 显示全部楼层
楼主没有设置:
PHP复制代码
$config['uri_segment'] = 3;
// 分页方法自动测定你 URI 的哪个部分包含页数。如果你需要一些不一样的,你可以明确指定它。
复制代码


楼主仔细看看 http://codeigniter.org.cn/user_guide/libraries/pagination.html

我觉得分页类非常好用,只不过不符合中国人的习惯而已,但中国人的习惯并不一定就是好习惯,呵呵
 楼主| 发表于 2010-7-26 18:04:56 | 显示全部楼层
回复 3# hj_545
这么重要的参数,官方居然漏写了。我不看源码还真不知道。
发表于 2010-7-26 18:14:18 | 显示全部楼层
确实,手册应该补充分页类的参数说明!
发表于 2010-7-26 18:42:11 | 显示全部楼层
回复 6# zmm1030


    哪个参数遗漏了?所有参数手册都有说明,请仔细阅读。

楼主这样写:
PHP复制代码
$config['cur_page'] = $this->uri->segment(5);//这里是获取动态页码,官方居然漏写,不看源码根本就不知道,还要传这个参数。这点很重要。
复制代码


这样写不对,请看我的回复,请不要使用手册里没有说明的参数,因为这个内部实现随时可能改变。
仔细阅读:http://codeigniter.org.cn/user_guide/libraries/pagination.html
发表于 2010-7-26 18:50:34 | 显示全部楼层
看错了~反正觉得这几个参数放在自定义那块不怎么合适~对于第一次使用或者说像测试分页类的来说,新建个自定义页面也不太合适~
个人因为应该直接说说cisystem library 下的pagination.php
 楼主| 发表于 2010-7-26 18:56:22 | 显示全部楼层
回复 8# Hex
请教我正确的使用方法,完整点,我比较笨~

本版积分规则