Codeigniter - Sort active recording in alphabetical order

I was wondering if anyone could help me with something.

I have an ajax bit that calls a function in my model.

But I, seemingly, cannot order an output on "model".

Below im function having problems with

function get_models_by_brand($tree = null)
{
    $this->db->select('id, model');

    if($tree != NULL){
        $this->db->where('brand_id', $tree);
    }

    $query = $this->db->get('models');
    $models = array();

    if($query->result()){
        foreach ($query->result() as $model) {
            $models[$model->id] = $model->model;
        }
        return $models;
    } else {
        return FALSE;
    }
}
+5
source share
1 answer

From the documentation ,

$ this-> db-> order_by ();

Lets you set the ORDER BY clause. The first parameter contains the name you want to order. The second parameter allows you to set the direction of the result. Parameters: asc or desc, or randomly.

$this->db->order_by("title", "desc"); 
// Produces: ORDER BY title DESC

You can also pass your own string in the first parameter:

$this->db->order_by('title desc, name asc'); 
// Produces: ORDER BY title DESC, name ASC

Or multiple function calls can be made if you need multiple fields.

$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc"); 
// Produces: ORDER BY title DESC, name ASC
+18

All Articles