代码点火器在哪里和or_where共存


Codeigniter where and or_where coexist?

我正在尝试在代码点火器中获得where()or_where(),以便与介于两者之间的AND一起工作。我希望我的查询如下所示。

SELECT fname 
FROM users 
WHERE is_active = 1 AND (
    employee_type = 'graduate' OR employee_type = 'staff' OR employee_type = 'Person Of Interest'
)

这是我当前的代码:

$this->db->select($print_data);
$this->db->from('users');
$this->db->where(array('is_active' => 1));
$where = '';
$this->db->or_where('employee_type', $search_criteria[0]);
for ($i = 1; $i < sizeof($search_criteria); $i++) {
    $this->db->or_where('employee_type', $search_criteria[$i]);
}

下面是我得到的输出。有人可以帮忙吗?

SELECT fname 
FROM users 
WHERE is_active = 1 OR employee_type1 = 'graduate' OR employee_type1 = 'staff' OR employee_type1 = 'Person Of Interest'`

在 CI 中使用 or_where 作为:

$this->db->select($print_data); 
$this->db->from('users'); 
$this->db->where(array('is_active' => 1)); 
$this->db->where('employee_type', $search_criteria[0]); // CHANGED
for ($i = 1; $i < sizeof($search_criteria); $i++) { 
    $this->db->or_where('employee_type', $search_criteria[$i]); 
}

第二种解决方案:

您也可以where_in .在这种情况下,从 0 开始循环:

$arr = array();
for ($i = 0; $i < sizeof($search_criteria); $i++) { 
    $arr[] = $search_criteria[$i];
}
$this->db->where_in('employee_type', $arr); // CHANGED