3

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?

4
  • The asked question is Unclear about the exact desired query logic. Commented Sep 6, 2024 at 12:43
  • I'm pretty confident that or_where_in() is not the solution to this problem. A simple where_in() will do what is required. Commented Mar 7 at 7:03
  • 1
    This question is similar to: Pass array to where in Codeigniter Active Record. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Mar 7 at 7:04
  • ->where_in('tbl_tapal.Tstatus', [$status, 2]) Commented Mar 11 at 20:23

1 Answer 1

6

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.