我在codeigniter中创建了一个分页,因为我总共有13条记录,我希望在每个页面中显示5条记录。当我点击分页链接号2时,它应该在URL中传递值2并显示接下来的5条记录,但我在url而不是2中获得值5.
我的控制器代码:
public function activities($page_num = 1){
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num))
$page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);
$data['activity_list'] = $this->skill_model->all_activities(1,$limit_start,$limit_end);
}
这是我的观看代码:
<?php echo '<div class="pagination" style="float:right;">'.$this->pagination->create_links().'</div>'; ?>
我的模特部分:
function all_activities($flag,$limit_start,$limit_end){
$this->db->select('*');
$this->db->from('skills_activities');
//echo $limit_start." ".$limit_end;
if($flag == 1 ){
$this->db->limit($limit_start, $limit_end);
$query = $this->db->get();
return $query->result_array();
} else {
$query = $this->db->get();
return $query->num_rows();
}
}
当我点击分页链接时,我收到以下网址:
http://192.168.1.97/projects/homecare/admin/skills/activities/5
但我应该得到:
http://192.168.1.97/projects/homecare/admin/skills/activities/2
我不知道我在哪里犯了错误。 有人可以帮助我吗?
提前致谢。
答案 0 :(得分:1)
$config['use_page_numbers'] = true;
这将在网址中产生页码而不是记录号。默认为false。
所以你的控制器看起来像这样
public function activities($page_num = 1)
{
$config =array();
$config['base_url'] = base_url().'admin/skills/activities';
$config['per_page'] = 5;
$config['use_page_numbers'] = true;//you missed this line
$config['total_rows'] = $this->skill_model->all_activities(0,'',''); // I will get the total count from my model
if(empty($page_num)) $page_num = 1;
$limit_end = ($page_num-1) * $config['per_page']; //end limit
$limit_start = $config['per_page']; // start limit
$this->pagination->first_url = $config['base_url'].'/1';
$this->pagination->initialize($config);