To order the result in order in your array, you can do the following:
$array_of_ordered_ids = array(4,5,2,6);
As you already know the order of the numbers, you can use the Mysql FIELD() Docs function:
ORDER BY FIELD(id, 4, 5, 2, 6);
To create such a string, you can use implode Docs :
$order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids));
Try:
$array_of_ordered_ids = array(4,5,2,6); $this->db->where_in('id', $array_of_ordered_ids); $order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids)); $this->db->order_by($order);
hakre source share