if ($for_category) {
$this->db
->select('category')
->from('categories')
->like('category',$for_category);
$subquery = $this->db->get_compiled_select();
}
$this->db->select('p.product,b.brand');
$this->db->from('products p');
$this->db->join('brands b','b.id = p.brand_id');
if ($for_category) {
$this->db->join('categories_to_product ctp','ctp.category_id = p.id');
$this->db->join('categories c','ctp.category_id = c.id');
$this->db->where("ctp.category IN ($subquery)",NULL,FALSE);
//not tested: $this->db->where_in('ctp.category',$subquery);
}
$this->db->like('p.product',$for_product);
$this->db->like('b.brand',$for_brand);
$query = $this->db->get();
if ($query->num_rows() > 0) {
$records = $query->result_array();
}