mysql - Codeigniter parentheses in dynamic Active Record query -


i'm producing query following using activerecord

select * (`foods`) `type` = 'fruits' ,         `tags` '%green%' or `tags` '%blue%' or `tags` '%red%' 

the number of tags , values unknown. arrays created dynamically. below added possible array.

$tags = array (                          '0'     => 'green'.         '1'     => 'blue',         '2'     => 'red' );   

having array of tags, use following loop create query posted on top.

$this->db->where('type', $type); //var type retrieved input value  foreach($tags $tag):               $this->db->or_like('tags', $tag); endforeach;  

the issue: need add parentheses around like clauses below:

select * (`foods`) `type` = 'fruits' ,        (`tags` '%green%' or `tags` '%blue%' or `tags` '%red%') 

i know how accomplish if content within parentheses static foreach loop throws me off..

from ci wiki:

the codeignighter activerecord feature allows create sql queries relatively , database-independant, there isno specific support including parenthesis in sql query.

for example when want statement come out simmilarly folowing:

where (field1 = value || field2 = value) , (field3 = value2 || field4 = value2)  

this can worked around feeding string ci->db->where() function, in case want escape values.

see following example:

$value=$this->db->escape($value); $value2=$this->db->escape($value2); $this->db->from('sometable'); $this->db->where("($field = $value || $field2 = $value)"); $this->db->where("($field3 = $value2 || $field4 = $value2)"); $this->db->get();  

a simmilar workaround can used clauses:

$this->db->where("($field '%$value%' || $field2 '%$value%')"); $this->db->where("($field3 '%$value2%' || $field4 '%$value2%')");  

Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -