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

后端 未结 5 2222
萌比男神i
萌比男神i 2020-11-29 02:32

I have a very small script to get all records from a database table, the code is below.

$query = $this->db->get($this->table_name);
return $query->         


        
相关标签:
5条回答
  • 2020-11-29 02:47

    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();
    
    0 讨论(0)
  • 2020-11-29 02:47

    Just add the'order_by' clause to your code and modify it to look just like the one below.

    $this->db->order_by('name', 'asc');
    $result = $this->db->get($table);
    

    There you go.

    0 讨论(0)
  • 2020-11-29 02:48

    Simple and easy:

    $this->db->order_by("name", "asc");
    $query = $this->db->get($this->table_name);
    return $query->result();
    
    0 讨论(0)
  • 2020-11-29 02:49
    function getProductionGroupItems($itemId){
         $this->db->select("*");
         $this->db->where("id",$itemId);
         $this->db->or_where("parent_item_id",$itemId);
    
        /*********** order by *********** */
         $this->db->order_by("id", "asc");
    
         $q=$this->db->get("recipe_products");
         if($q->num_rows()>0){
             foreach($q->result() as $row){
                 $data[]=$row;
             }
             return $data;
         }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-29 03:04

    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()

    0 讨论(0)
提交回复
热议问题