I am new to Codeigniter and I am trying to figure out a way to use pagination and also filter some data. I have pagination setup to show all records in a table. I want to be able to filter those records using a specific column in the table. The column is an int
. My controller is clients
and the method is index
so going to http://localhost/clients
will produce a list of clients in a table. When I go to another page to display more results the URL changes to something like http://localhost/clients/50
depending on what page I am on. Now, I have one argument for my controller method which is $client_status
, which again is an int
. Since CI is using the second segment of the URL for pagination, how to I pass the argument for filtering by client status? Here is what the code looks like:
public function index($client_status = false) {
if(!$client_status) {
$data['clients'] = $this->clients_model->list_clients($config["per_page"], $page);
} else {
$data['clients'] = $this->clients_model->list_clients($config["per_page"], $page, $client_status);
}
And my model:
public function list_clients($limit, $start, $client_status = false) {
if(!$client_status) {
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('clients');
$this->db->join('client_status', 'clients.client_status_id = client_status.client_status_id');
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
} else {
$this->db->limit($limit, $start);
$this->db->select('*');
$this->db->from('clients');
$this->db->join('client_status', 'clients.client_status_id = client_status.client_status_id');
$this->db->where(array('clients.client_status_id' => $client_status));
$query = $this->db->get();
if($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
}
To do this you will have to pass your parameters into the query string. In your config for the pagination class make sure to set these following parameters
$config = array(
'page_query_string' => TRUE,
'query_string_segment' => 'page', // the query string param for the page numbers
'reuse_query_string' => TRUE,
)
With the config your URL's would be similar to