How to add an ORDER BY clause using CodeIgniter's Active Record methods? How to add an ORDER BY clause using CodeIgniter's Active Record methods? codeigniter codeigniter

How to add an ORDER BY clause using CodeIgniter's Active Record methods?


I believe the get() function immediately runs the select query and does not accept ORDER BY conditions as parameters. I think you'll need to separately declare the conditions, then run the query. Give this a try:

$this->db->from($this->table_name);$this->db->order_by("name", "asc");$query = $this->db->get(); return $query->result();

CodeIgniter Documentation order_by()


Using this code to multiple order by in single query.

$this->db->from($this->table_name);$this->db->order_by("column1 asc,column2 desc");$query = $this->db->get(); return $query->result();


Simple and easy:

$this->db->order_by("name", "asc");$query = $this->db->get($this->table_name);return $query->result();