【问题标题】:Multiple Like Clauses Codeigniter多个类似子句 Codeigniter
【发布时间】:2017-05-15 08:42:16
【问题描述】:

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

型号:

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');
     }
}

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    您使用的 or_like ci 方法会打印“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();
            }
        }
    

    【讨论】:

    • 如果两个结果都执行相同的操作,您为什么还要费心编写if 块?此外,->select('*') 是不必要的。为什么要将方法链接与非链接技术混合使用?
    【解决方案2】:
     $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();
    

    【讨论】:

    • 您好,感谢您的回答,但是当我按下搜索按钮时,现在它不显示任何记录
    • 保留 print_r($this->db->last_query);在 $query = $this->db->get();在这一行中,您将获得一次查询检查并在数据库中执行一次,您将确切地知道错误发生的地方..
    • 现在,它显示 SELECT * FROM 'accounts' WHERE 'role' =0 AND 'isActive' = 1 AND 'firstname' = 'LIKE' AND 'lastname' = 'LIKE' AND 'username' = 'LIKE' AND 'address' = 'LIKE'$searchquery 似乎没有被阅读
    • 为此写非空条件
    猜你喜欢
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-04-30
    • 2016-07-24
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多