phpcodeigniterwhere-clausequery-builderlogical-grouping

how to use multiple or_where on same field in codeigniter


I need to run a query with conditions of where and or_where on the same field using codeigniter. There are where operations performed other fields to. The code,

function get_newTapal($off, $status)        
{   
  $p = $this->db->select('*')
        ->from('tbl_tapal')
    ->join('tbl_subjectmain', 'tbl_subjectmain.Msub_Cd = tbl_tapal.Tmain_sub')
    ->join('tbl_subject_all', 'tbl_subject_all.Sub_Cd = tbl_tapal.Tsub_sub')
    ->join('tbl_seat', 'tbl_seat.SeatCd = tbl_tapal.Tseat')
    ->where('tbl_tapal.Tstatus', $status)
    ->where('tbl_tapal.Toffice_id', $off)
    ->where('tbl_tapal.Tmain_sub !=', 1)
    ->where('tbl_tapal.Tsub_sub !=', 2) 
    ->or_where('tbl_tapal.Tstatus', 2)
    ;
    $query = $p->get();
    $new_tapal=$query->result();    
    foreach($new_tapal as $row){    echo $row->Toffice_id.'/'.$row->Tapal_no.'/'.$row->Tyear.'<br>';    }
}

When i run the query without the or_where condition it woks properly according to the code. I need the rows which satisfies all where conditions where the status value is either '1' or '2'. But when i add the or_where condition it doesn't check the other conditions at that part of query. i.e, The query returns rows satisfying first 4 where conditions together plus the rows which satisfies just the or_where condition.
Could someone help me about that?


Solution

  • You can use $this->db->or_where_in() for checking the multiple or condition in where.

    Ex:

    $status= array('1', '2', '3');
    $this->db->or_where_in('tbl_tapal.Tstatus', $status);