1
votes

in Codeigniter This is my Controller => wishlist function

  $data = array(
  'user_id' => $this->_user_id
  );
  $order_by = "id";
  $query = $this->Database_model->get_data('wishlist','*', $data, $order_by);

  print_r($query);

RESULT:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [item_id] => 2
            [user_id] => 2
            [tarix] => 2018-03-13 00:00:00
        )

    [1] => stdClass Object
        (
            [id] => 2
            [item_id] => 15
            [user_id] => 2
            [tarix] => 2018-03-13 00:00:00
        )

    [2] => stdClass Object
        (
            [id] => 3
            [item_id] => 10
            [user_id] => 2
            [tarix] => 2018-03-13 00:00:00
        )

)

And now I want to get all "items" table datas WHERE id = all these "item_id" inside array.

I tried to create new array and push all item_id inside it, using foreach, but in Codeigniter I can't give it to query.

1
Are you saying you want a where in statement? - Kisaragi
You could use the query that built this as a subquery of your next query. where id in(select id ...) - chris85
@Kisaragi yes where x=y or z=u and etc like this - NurlanXp
To pass an array for where in your query needs to use $this-db->where_in('column', $array); Please edit your question to include get_data() - Kisaragi
I don't know Codeigniter but you can use a subquery in mysql easily enough which should give you what you want. Roughly select * from items where id in (select item_id from wishlist where ...) - chris85

1 Answers

2
votes

You can do it like this: In your controller

$data = array(
    'user_id' => $this->_user_id
);
$order_by = "id";
$query = $this->Database_model->get_data('wishlist','*', $data, $order_by);

$where = array();
foreach ($query as $key => $value)
    array_push($where, $value->item_id); //  Must be item_id

$my_items = $this->Database_model->get_custom_items('items', $where);

In your model

public function get_custom_items($table, $where)
{
    $this->db->select('*');
    $this->db->where_in('id', $where); // Must be id
    return $this->db->get($table)
}