1
votes

I want to pass array to where_in using Active Record.

I saw few reference link, can pass like this:

$this->db->where_in('id', array('20','15','22','42','86'));

Query :

$mts = $this->db->select('subcat_id')->get_where('subcategory_tbl', array('cat_id' => $cat_id));
$result = $mts->result_array();

But My result Array in this form :

Array
(
    [0] => Array
        (
            [subcat_id] => 12
        )

    [1] => Array
        (
            [subcat_id] => 13
        )
)

So how can i pass this array id to where_in clause ? I am confused here.

4

4 Answers

0
votes

You can do it like:

$this->db->where_in('cat_id', $cat_id)
$this->db->select('subcat_id');
$this->db->get('subcategory_tbl');

assuming that $cat_id contains the array of values to check.

0
votes
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
0
votes

You need to convert your result (array of rows) to an array of the values in the first "column" of those rows:

$ids = array_map(function($row) { return $row['subcat_id']; }, $result);

You probably should check $ids isn't empty before using it - since (I can't remember but I think) where_in will happily generate the invalid

SELECT ... WHERE id IN ()

if you pass it an empty array.

0
votes

Use your first query like

$mts = $this->db->select('GROUP_CONCAT(subcat_id) as new')->get_where('subcategory_tbl', array('cat_id' => $cat_id))->row()->new;

and then use

if($mts != "")
{
    $this->db->where_in('id',  $mts);
}

Hope it helps you....