3
我是Codeigniter的新手,我試圖找出使用分頁方式並過濾一些數據的方法。我有分頁設置來顯示錶中的所有記錄。我希望能夠使用表中的特定列來過濾這些記錄。該列是int
。我的控制器是clients
,方法是index
,所以去http://localhost/clients
會產生一個表中的客戶列表。當我轉到其他頁面顯示更多結果時,URL會根據我所處的頁面而變化爲http://localhost/clients/50
之類的內容。現在,我的控制器方法有一個參數是$client_status
,它又是一個int
。由於CI正在使用URL的第二部分進行分頁,因此如何通過參數進行客戶端狀態篩選?下面是代碼的樣子:在使用分頁時過濾數據Codeigniter
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);
}
而且我的模型:
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;
}
}
}