Where or in codeigniter php

Hello,

Today I will tell you how to use where or in codeigniter.

There are 2 ways to resolve this problem.

  1. You can use where something like this:
    $this->db->where('(field LIKE %value1% OR field LIKE %value2%)', null, false);
  2. The second way I used to generate your or where clauses.
    For example you have an array $arr = array(1,2,3); and you want to create a query where you want to check if active = 1 and if id_parent equals 1 or 2 or 3. To resolve that you can do something like this

    $this->db->group_start();
    $this->db->where('active','1');
    foreach($arr as $k=>$v) {
          if($k == 0) {
                $this->db->where('id_parent',$v);
          } else {
                $this->db->or_where('id_parent',$v, NULL, FALSE);
          }
    }
    $this->db->group_end();

This will result in a query containing
WHERE active = '1' AND (id_parent = 1 OR id_parent = 2 OR id_parent = 3)

You can choose what method suites you better to use where or in your codeigniter query.