I have a string with image ids (fetched from another mysql table) and converted to an array:
$idstring = "12, 18, 3, 392, 0, 9, 44";
$idarray = explode(',', $idstring);
Based on this array of ids, I want get all the rows from my "media" mysql table.
$result = $this->db->select('*')
->from('media')
->where_in('id', $ids)
->get()->result_array();
The problem is the $result
array's values are in a weird order like this:
$result's order : 44, 9 ,0 ,18 ,3 ,392 ,12 ...
But i need them to stay like in my $id string/array order...
I've tried 4 approaches to solve the issue so far:
Fetch rows in a loop without
where_in()
- what creates a lot of queries - but works for now ...Reorder the
$result
array based on the order of the$idstring
or the$idarray
, though I could not manage to to find a working result and I don't get the point why this step is necessary at allTry to get the query itself fixed. I've heard about
ORDER_BY
andFIND_IN_SET
,$ids
but I could not get it into my a working codeigniter query and don't know about the performance if this is really a help
So in conclusion, I think this should be a simple everyday task, i just want to fetch a bunch of pictures in a given order with codeigniter.
Am I missing a simple solution here?