user3454007
user3454007

Reputation: 33

How to sort database values in descending order with CodeIgniter?

I am bit new to CI.I want to sort my database values in descending order using this query below in my model. However, it doesn't work form me and throw errors .please help me out.

   function get_records(){      
    $this->db->from($this->tbl_contactus);
    $this->db->order_by("contactus_id", "asc");
    $query = $this->db->get();
    return $query->result();
}

The error: enter image description here

Upvotes: 2

Views: 14824

Answers (2)

Walter White
Walter White

Reputation: 86

Try like this

   function get_records(){

    $this->db->select("*");
    $this->db->from("tbl_contactus");
    $this->db->order_by("contactus_id", "desc");
    $query = $this->db->get();
    return $query->result();

}

and also you can use this

function get_records(){

    $this->db->select()->from('tbl_contactus')->order_by('contactus_id', 'desc');

}

Upvotes: 3

echo_Me
echo_Me

Reputation: 37233

you have to check $tbl_contactus because its not defined or empty in that line

  $this->db->from($this->tbl_contactus);

You are using query like that:

    SELECT * ORDER BY contactus_id ASC

while it should be

    SELECT * FROM your_table ORDER BY contactus_id ASC

Upvotes: 1

Related Questions