0
$this->db->where('column_field1',$value1);
$this->db->where('column_field2',$value2);

For above two query we can write a single query as:

$arr = array('column_field1'=>'value1', 'columne_field2'=>'value2');

function select($arr){
    .. .. ..
    ... .. . ..
    $this->db->where($arr);
}

is there any solution for the where_in query:

function($value1, $value2){
   $this->db->where_in('column_field1',$value1);
   $this->db->where_in('column_field2',$value2);
}

as I have tried but didn't work:

arr = array('column_field1'=>$arr,'column_field2'=>arr2);

function select_in($arr)
 {
        $this->db->select('*');
        $this->db->from('table');
        $this->db->where_in($arr);
        $query = $this->db->get();
        return $query;
    }

I want to combine the where_in condition so that i can store multiple column_field and array for it. $this->db->where_in($arr); where $arr contains pair of column_filed and array_of_value: $arr = $array('column_field'=>$arr_of_value);

0

3 Answers 3

1
function select_in($arr)
 {
        $this->db->select('*');
        $this->db->from('table');
        $this->db->where($arr);  // change here
        $query = $this->db->get();
        return $query;
    }

If you want multiple where In then you need to write it twice....It's not possible in single statement.

$this->db->where_in('field1',$cond1);
$this->db->where_in('field2' , $cond2);

Note: Where_in is similar to where id IN (1,2,3...)but in your case you are doing multiple where condition.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to combine multiple where_in() condition
even i cant pass an array in where_in() condition like: $this->db->where_in($arr);
edited please check
not possible? sure..?
yap according to it's source code it is not possible
1

Creating custom method is a reasonable solution for this, you can either extend database's active record class or write custom function which takes array and call where_in like below

function multiple_where_in($array){
    foreach($array as $key  => $data){
        $this->db->where_in($key, $data);
    }
  }

And call like below

function select_in($arr)
{
        $this->db->select('*');
        $this->db->from('your_table');
        $this->multiple_where_in($arr);  // call local function created above
        $query = $this->db->get();
        return $query;
}

// create array like below, fieldname and field_values inside array
$arr = array(
              'column_field1'=>array('value1','value2'),  
              'columne_field2'=>array('value2','value3')
);

// call your select method
$this->select_in($arr);

Comments

0

You can try this solution for your problem.

    $this->db->select('*');
    $this->db->from('table');
    if(!empty($cond1) && !empty($cond2)){
        $this->db->where("field1 IN (".$cond1.") AND field2 IN (".$cond2.") ",null,false);    
    }
    $query = $this->db->get();
    return $query;

I hope this will helps you. Thanks!

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.