关于分页的问题
情况大概是这样的:我用CI做一个网上书店这样的系统,有一个功能是查看某一分类下所有书籍,比如说小说的分类号是10001,访问方式是 http://localhost:8080/category/show/10001,其中category是我的控制器名称,show是用户显示信息的一个成员函数。但是在用CI自带的分页功能的时候就出现了问题,输入http://localhost:8080/category/show/10001/1 第一页可以显示,但点击“第二页”链接的时候却跳转到了http://localhost:8080/category/show/2
是不是分页时的 $config['base_url']= 设置不正确呢?
我设置的是http://localhost:8080/category/show/ 应该把 10001 也放入 base_url 中,并且设置 $config['uri_segment'] = 4; $config['uri_segment'] = 4; 我之前已经设置了。
但是这个10001这一段是个变量呀,是由读取数据库数据来动态生成的值而不是一个常量,我该怎么做呢? 本帖最后由 yinzhili 于 2009-4-17 20:15 编辑
看了http://codeigniter.org.cn/forums/thread-298-1-42.html 后终于明白了,写成 $config['base_url'] = base_url().'category/show/'.$cate_id.'/'; 终于可以正确访问了。 本帖最后由 yinzhili 于 2009-4-18 10:39 编辑
出现了新的问题,生成的导航页码链接不对,比如说生成的导航页码是这样的:
12 3 下一页,当显示第二页的时候结果是正确的,但链接错位了。
比如说 http://localhost:8080/category/show/4/2 这是第二页的URL,然而单击第三页的链接"3"的时候却跳转到了http://localhost:8080/category/show/4/4 ,单击“上一页”时跳转到了 http://localhost:8080/category/show/4/ ,整个就乱了套了。
附上相关代码:
//这是控制器里的相关代码
$cate_id=$this->uri->segment(3);
$this->load->library('pagination');
$config['base_url'] = base_url().'category/show/'.$cate_id.'/';
$config['total_rows'] = $this->book_model->count_rows($cate_id); //count_rows函数会返回结果的总行数
$config['per_page'] = '2';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';
$config['uri_segment'] = 4;
$config['first_link'] = '首页';
$config['last_link'] = '末页';
$config['next_link'] = '下一页';
$config['prev_link'] = '上一页';
$config['cur_tag_open'] = "<font color='Red'><b>";
$config['cur_tag_close'] = "</b></font>";
$this->pagination->initialize($config);
$data['book_list'] = $this->book_model->get_category_detail($cate_id,$config['per_page'],$this->uri->segment(4));
$this->load->view('category_view',$data);
//这是模型里的相关代码
function count_rows($category_id){
$query_string="SELECT * FROM books,categories WHERE books.category=categories.cate_name AND categories.cate_id='{$category_id}'";
$query = $this->db->query($query_string);
if(($query->num_rows())!=0){
return $query->num_rows();
}
}
function get_category_detail($category,$num,$offset){
$query_string="SELECT * FROM books,categories WHERE books.category=categories.cate_name AND categories.cate_id={$category} LIMIT {$offset},{$num}";
$query = $this->db->query($query_string);
if( ($query->row_array())==null ){
return null;
}
else{
$result=$query->result();
return $result;
}
}
CI 里的当前页是数据库记录偏移量,不是页码。 明白了,谢谢您的指点。
页:
[1]