Hi have am new to codeigniter and have been struggling with CI pagination library for days.
I have this app where all pagination are working fine except for skipping data by offset.
i.e. when I click on next, It only shifts data by 1 record only. Example: From 1-10 showing it goes to 2-11 and so on..
Here is my controller:
$active = 1;
$total = $this->all_users_model->total_number_of_rows();
$per_page = 10;
$offset = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$config['base_url'] = base_url().'admin/manage-users/';
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['use_page_numbers'] = TRUE;
$config['num_links'] = $total;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
$config['first_link'] = 'First';
$config['prev_link'] = 'Previous';
$config['next_link'] = 'Next';
$config['last_link'] = 'Last';
$this->pagination->initialize($config);
$data['admin_all_users'] = $this->all_users_model->show_all_user_details($active, $per_page, $offset);
Model:
public function show_all_user_details($active,$per_page,$offset) {
$this->db->where(array('activated'=>$active,'admin !='=>1));
$this->db->limit($per_page,$offset);
$this->db->order_by('api_id','desc');
$query=$this->db->get('registered_members');
return $query->result();
}
It's because your offset is incrementing only with one at a time: page/1
, page/2
, also as your db query limit. Currently, your db limit is $this->db->limit(10, 1);
, $this->db->limit(10, 2);
.
This shows 10 records from record 1, and then from record 2.
Just change the limit in your model to:
$this->db->limit($per_page, $offset * $per_page);