phpcodeignitercodeigniter-query-builder

Can I add an IF condition in between query builders of codeigniter


I don't know what they are exactly called but I call the query builders $this->db->select().

Is it possible to do this:

$this->db->select('*')
         ->from('table_name')
         if(1)
         {
          ->where('column_name',1)
         }
         ->order_by('column_name','ASC');

If not are there any alternatives I can do?

Note: I just want to make my code short and not create another function where the only difference is that there is a where() in the function.


Solution

  • It is possible to do this:

    $this->db->select('*')->from('table_name');
    if(1)
    {
        $this->db->where('column_name',1);
    }
    $this->db->order_by('column_name','ASC');