php - Conditional clauses using Codeigniter's Active Record -


i have function returning data based on parameters passed. basically, queries change depending on clauses.

if(1==$case)  return select()->from('db')->where('x',$something)->where('y','fixed')...  else if(2==$case) return select()->from('db')->where('x','fixed')->where('y',$something)... 

is there better way of doing this?

also, possible have such clause

...->where('category',*) 

'*' replaced value or equates "any".

this type of queries called "dynamic queries" because can combine clauses in easy way. try this:

$query = $this->db->select()     ->from('db'); if(1==$case) {     $query->where('x',$something)           ->where('y','fixed') } elseif (2==$case){     $query->where('x','fixed')           ->where('y',$something) } .... return $query->get(); 

answering second question, can use:

$this->db->like('category', '%'); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -