4

Is it possible to sort the values retrieved from MySQL, in say descending id?

Thanks.

2
  • 1
    I have no idea what codeigniter is, but "order by id desc"? :) Commented Aug 20, 2010 at 10:35
  • Yeah, this should really be done in the query, not afterwards. Commented Aug 20, 2010 at 10:39

3 Answers 3

7

Here you go...

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

Read more over in codeigniter documentation for active record class.

Sign up to request clarification or add additional context in comments.

Comments

5

As ShiVik suggested, you can do this via the Active Record class quite easily. Also note that you can chain your queries together if you are using PHP 5+:

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

Comments

2

An exemple:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.