CodeIgniter Forums
[Active Record] Order by field - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: [Active Record] Order by field (/showthread.php?tid=67772)



[Active Record] Order by field - boddah85 - 04-07-2017

Hi there

I'm using CI 3.1.4 and have a problem with 'order by field'.

Code:
...
$this->db->where_in('table.column',$artIDs);
$order = sprintf('FIELD(table.column, %s)', implode(', ', $artIDs));
$this->db->protect_identifiers(FALSE)->order_by($order);
$this->db->limit((int)$limit,(int)$start);

 return $this->db->get();

I'm propably doing something wrong. Can anyone help me? Thanks in advance.


RE: [Active Record] Order by field - neuron - 04-07-2017

I suggest u to output your query as a string and try that query in mysql workbench, that way is easier to see errors
PHP Code:
$this->db->where_in('table.column',$artIDs);
$order sprintf('FIELD(table.column, %s)'implode(', '$artIDs));
$this->db->protect_identifiers(FALSE)->order_by($order);
$this->db->limit((int)$limit,(int)$start);


$sql $this->db->get_compiled_select();
echo 
$sql;
return 
$this->db->query($sql);