Codeigniter - sort received values ​​from MySQL

Is it possible to sort values ​​obtained from MySQL, for example, in descending order of id ?

Thanks.

+4
source share
3 answers

Here you go ...

 $this->db->select("*"); $this->db->from("table"); $this->db->order_by("id", "desc"); $this->db->get(); 

See the codeigniter documentation for the active writer class for more details.

+7
source

As suggested by ShiVik, you can easily do this with the Active Record class. Also note that you can link your queries together if using PHP 5 +:

 $this->db->select('*')->from('table')->order_by('id', 'desc'); $query = $this->db->get(); 
+5
source

Example:

 $query = $this->db->order_by("id", "desc")->get('table'); if($query->num_rows>0){ foreach ($query->result() as $row){ $names[] = $row->name; } return $names; } 
+2
source

All Articles