phpmysqlcodeigniteractiverecordsql-order-by

Implement sorting with CodeIgniter active record without refactoring


$orders = $this->order->with("customer")->with("address")->get_many_by(array("submitted" => 1));
$this->main = $this->load->view("partials/admin/list_orders", array("allorders" => $orders), true);

This is my query, there is a column in table orders named as time, which includes date and time, I want query designed in such a way that returned results are sorted desc on basis of column time, can any one please help me or tell my how to do that?


Solution

  • if you have a table like

    id     |     name      |    date                  |  logic_erased
    1           juan           2016-06-29 14:20:03          0
    2           alfred         2016-06-29 14:19:58          0
    

    and you want to sort date desc

    $this->db->order_by('date', 'desc');
    
    return $this->db->get_where('table', array('logic_erased' => 0))->result()