Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

How to pass an array to where condition in CodeIgniter?

I am using CodeIgniter in my application. I have a code like this

$this->db->where('a.PreferenceID NOT IN (SELECT PreferenceParentID FROM '.$table.' WHERE EntityID='.$shop_id.')', NULL, FALSE);

I want to pass an array instead of $shop_id.

Like

$arr = array(1,3,5);

WHERE EntityID='. $arr.'

it will be equal to

WHERE (EntityID='. $arr[0].' OR EntityID='. $arr[1].' OR EntityID='. $arr[2].')

Because I want to check with multiple shop id's. What is the best way?

Upvotes: 1

Views: 2569

Answers (3)

Philip
Philip

Reputation: 4592

Try something like

WHERE EntityID IN('.implode(',', $arr).')

Upvotes: 0

Uday Sawant
Uday Sawant

Reputation: 5798

use IN clause like

$this->db->where('a.PreferenceID NOT IN (
    SELECT PreferenceParentID FROM '.$table.' 
    WHERE EntityID IN ('.implode(",", $array).'))',
    NULL, FALSE
);

Upvotes: 3

Gaurav
Gaurav

Reputation: 28755

use

'SELECT PreferenceParentID FROM '.$table.' WHERE EntityID IN ('.implode(',', $arr_id).')'

Upvotes: 1

Related Questions