Reputation: 175
I'm having a problem with pagination in codeigniter. When I apply filters to reduce the recordset the pagination seems to think that it is still dealing with the unfiltered data. So for example, although there is only one page of data in the returned dataset, there are still 3 pagination links displayed. Clicking on any other than the first one will return a blank page.
Here is my controller code
function pro_client_list() {
//called to open the List tab of the pro_clients_view
$this->load->library('pagination');
//this version is for localhost
$config['base_url'] = '/index.php/main_controller/pro_client_list/';
$config['total_rows'] = $this->pro_client_model->count_clients();;
$config['per_page'] = 20;
$config['full_tag_open'] = '<div class="pagination"><ul>';
$config['full_tag_close'] = '</ul></div>';
$this->pagination->initialize($config);
$data = array();
// DEV-140905A - check the state of the filters and only load matching clients
$this->load->model('filter_model');
$this->load->model('pro_client_model');
$c_filter = $this->filter_model->get_client_category_filter();
$y_filter = $this->filter_model->get_client_country_filter();
$data['clients'] = $this->pro_client_model->get_clients($config['per_page'], $this->uri->segment(3), $c_filter, $y_filter);
$this->load->model('filter_model');
$data['categories'] = $this->filter_model->get_unique_categories();
$data['countries'] = $this->filter_model->get_unique_countries();
$this->load->view('includes/header');
$this->load->view('pro_client_list', $data);
$this->load->view('includes/footer');
}
and this is the code for the model
function get_clients($num, $offset, $c_filter, $y_filter) {
/*
Returns a list of all clients in the database with pagination pointers
Modified to return a count of the contacts belonging to each client 04/09/2014
*/
$this->c_filter = $c_filter;
$this->y_filter = $y_filter;
$this->db->order_by("vch_name", "asc");
$this->db->select('*,
(SELECT COUNT(*) FROM tbl_contact WHERE fk_client_id=tbl_pro_client_id) AS count_contacts,
(SELECT MIN(vch_next_interact) FROM tbl_contact WHERE fk_client_id=tbl_pro_client_id) AS next_interact'
, false);
if($c_filter != '-1') {
$this->db->where('vch_category', $this->c_filter);
}
if($y_filter != '-1') {
$this->db->where('vch_country', $this->y_filter);
}
$query = $this->db->get('tbl_pro_client', $num, $offset);
return $query;
}
Please not that the pagination was working fine prior to adding the Category and Country filters.
Any help greatly appreciated,
U.
Upvotes: 0
Views: 178
Reputation: 175
Ok, I'm afraid this simply comes down to brain trouble. In setting up the pagination $config I have a line
$config['total_rows'] = $this->pro_client_model->count_clients();
Of course, if I apply a filter then I need to re_calculate the row count, so I modified the function to use the same filters as the display function. It now looks like this:
$config['total_rows'] = $this->pro_client_model->count_clients($c_filter, $y_filter);
Works a treat!
I really shouldn't try coding this early in the morning before the caffeine kicks in :-)
Thanks for looking at this.
U.
Upvotes: 0