How to store query(conditions) in variable using codeigniter

Viewed 170

I want to store query conditions in variable, beacuse same conditions can be used in different places so I created on function and used concatenation, but it is not storing any query condition.

Code:

 $records; // empty varaible to store all conditions using concatenation
     if ($val->id != '')
        $records .=$this->db->where('table.column', $val->id, true);
    
    if ($val->percentage != '')
        $records .=$this->db->or_where('table.column', $val->percentage, true);

  return $records;
1 Answers

Try this

$query = $this->db->from('table')
if ($val->id != '')
    $query = $query->where('table.column', $val->id, true);
if ($val->percentage != '')
    $query = $query->or_where('table.column', $val->percentage, true);
$results = $query->get()->result();
Related