Reputation: 39
I have this query that works fine on mysql, but I can't get it to work in CodeIngniter's Query Builder Class. It's a search box with multiple inputs.
I've tried this, but couldn't make it work.
$this->db->like('num_articulo ', $aricle_number, 'after');
$array = array('descripcion1' => $article_description, 'descripcion2' => $article_description)
$this->db->or_like($array);
$this->db->where('status', 1);
return $this->db->get('articulos')->result_array();
Here is my sql
SELECT * FROM articulos WHERE num_articulo LIKE 'yyyyy%' AND (descripcion1 LIKE '%xxxx%' OR descripcion2 LIKE '%xxxx%')
Upvotes: 0
Views: 374
Reputation: 853
CodeIgniter Query Builder has a neat query grouping feature:
$this->db->group_start()
$this->db->group_end()
Hence, you're query would look like, in a pure QB fashion:
$this->db->like('num_articulo ', $aricle_number, 'after');
$this->db->group_start();
$this->db->like('descripcion1', $article_description);
$this->db->or_like('descripcion2', $article_description);
$this->db->group_end();
$this->db->where('status', 1); // This one doesn't seem to appear in your query example?
$this->db->get('articulos')->result_array();
You would get the following query:
SELECT * FROM `articulos` WHERE `num_articulo` LIKE 'yyyyy%' ESCAPE '!' AND ( `descripcion1` LIKE '%xxxx%' ESCAPE '!' OR `descripcion2` LIKE '%xxxx%' ESCAPE '!' ) AND `status` = 1
Note: CodeIgniter adds ESCAPE '!'
internally to identify the escape symbol.
Upvotes: 1
Reputation: 39
might not be the nicest thing to do, but it worked just fine.
$this->db->like('num_articulo', $number, 'after');
$this->db->where('status', 1);
$this->db->where('(descripcion1 LIKE "%'.$search_data.'%" OR descripcion2 LIKE "%'.$search_data.'%")');
return $this->db->get('articulos')->result_array();
Upvotes: 0