多个喜欢条款Codeigniter

时间:2016-12-31 07:53:40

标签: php mysql sql codeigniter

我正在尝试为我的网站实施搜索框。在我的用户帐户表中,我将帐户划分为三个,即房主,管理员和停用的用户。我希望搜索功能能够搜索多个列(按用户名,名字,姓氏,地址,名字和姓氏的组合搜索)。但是在我的模型中,我的WHERE子句似乎被忽略了。在我的search_homeowner函数中,我将结果限制为仅显示房主,但我得到显示管理员的结果。我似乎没有多个LIKE,所以我尝试了OR喜欢。这是代码。

型号:

function search_homeowner($searchquery) {

    $this->db->select('*')->from('accounts')->where('role', 0)-> where('isActive', 1);
    $this->db->like('firstname',$searchquery,'after');
    $this->db->or_like('lastname',$searchquery,'after');
    $this->db->or_like('username',$searchquery,'after');
    $this->db->or_like('address',$searchquery,'after'); 

    $query = $this->db->get();
    if($query->num_rows() > 0) {
        return $query->result();
    } else {
        return $query->result();
    }
}

控制器:

function search_homeowner() {
     $this->load->model('model_accounts');
     $searchquery = $this->input->post('search');

     if(isset($searchquery) and !empty($searchquery)) {
        $data['users'] = $this->model_accounts->search_homeowner($searchquery);
        $data['main_content'] = 'view_adminaccounts';
        $data['homeownerlinks']='';
        $this->load->view('includes/admin_accounts_template', $data);
     } else {
        redirect('admin_accounts/homeowner');
     }
}

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

 use this query hope it will help for you.

 $whereaccounts=array('role'=>0,'isActive'=>1);
 $this->db->select('*')->from('accounts')->where($whereaccounts);
 $this->db->where('firstname', 'LIKE', '%' . $searchquery . '%');
 $this->db->where('lastname', 'LIKE', '%' . $searchquery . '%');
 $this->db->where('username', 'LIKE', '%' . $searchquery . '%');
 $this->db->where('address', 'LIKE', '%' . $searchquery . '%');
 $query = $this->db->get();

答案 1 :(得分:0)

你正在使用的or_like ci方法是print" searchstring%"。如果你删除了它,将会像这样打印"%searchstring%"。所以请尝试这样

   function search_homeowner($searchquery) {
        $this->db->select('*')->from('accounts')->where('role', 0)-> where('isActive', 1);
        $this->db->like('firstname',$searchquery);
        $this->db->or_like('lastname',$searchquery);
        $this->db->or_like('username',$searchquery);
        $this->db->or_like('address',$searchquery); 

        $query = $this->db->get();
        if($query->num_rows() > 0) {
            return $query->result();
        } else {
            return $query->result();
        }
    }