|
本帖最后由 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 |
|