Hello everyone, there is such a request (part of the request)

$this->db->where('id_category',$cID); $this->db->where('id_subcategory',$scID); $this->db->where('visible',1); if(isset($filter_brand)): if(count($filter_brand)): foreach ($filter_brand as $item): foreach ($item as $item2): $this->db->where('brand',$item2); endforeach; endforeach; endif; endif; $query = $this->db->get('tm_product_'.$city); 

WHAT is the same as

 SELECT * FROM (`tm_product_almaty`) WHERE `price` >= '2' AND `price` <= '2111' AND `id_category` = '1' AND `id_subcategory` = '16' AND `id_category` = '1' AND `id_subcategory` = '16' AND `visible` = 1 AND `brand` = 'ЭЛИТАН' ORDER BY `title` LIMIT 12 

I have a problem when I select two brands, then a request is generated for the similarity of this

 AND `brand` = 'Бренд 1' AND `brand` = 'Бренд 2' 

And I need to have a request like this

 SELECT * FROM (`tm_product_almaty`) WHERE `price` >= '2' AND `price` <= '2111' AND `id_category` = '1' AND `id_subcategory` = '16' AND `id_category` = '1' AND `id_subcategory` = '16' AND `visible` = 1 AND (`brand` = 'Брэнд 1' or `brand` = 'Брэнд 2') ORDER BY `title` LIMIT 12 

How to implement it

2 answers 2

If I understand correctly, then you have

 foreach ($item as $item2): 

$item is an array, then you can use

 $this->db->where_in('brand', $item); 

Sample code would be

 $this->db->where('id_category',$cID); $this->db->where('id_subcategory',$scID); $this->db->where('visible',1); if(isset($filter_brand)): if(count($filter_brand)): foreach ($filter_brand as $item): $this->db->where_in('brand', $item); endforeach; endif; endif; $query = $this->db->get('tm_product_'.$city); 

    Taken from reference :

    $this->db->or_where()

    This function is identical to the one above, except that there are several instances:

    $this->db->where('name !=', $name);

    $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50