Search code examples
phpcodeigniteractiverecordquery-builderlogical-grouping

Codeigniter query to correctly encapsulate AND and OR conditions in a WHERE clause


I want a CodeIgniter select query from a table with three conditions.

  1. wrk_fld_exc = 140
  2. wrk_cs_sts = 'Open'
  3. wrk_dlvrd_sts = 'Delivered' OR wrk_cl_sts = 'Success'

The third condition is an AND condition contains an OR condition. First and second is an AND condition.


Solution

  • You can code it like this:

           $this->db->where('wrk_fld_exc',140);
           $this->db->where('wrk_cs_sts','open');
           $where = '(wrk_dlvrd_sts="open" or wrk_cl_sts = "Success")';
           $this->db->where($where);