9

I was wondering if someone could help me out with something.

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

But i cant seem to be able to order the output by 'model'.

Below the function im having trouble 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;
    }
}
4

1 回答 1

37

从文档中,

$this->db->order_by();

允许您设置 ORDER BY 子句。第一个参数包含您要排序的列的名称。第二个参数允许您设置结果的方向。选项是 asc 或 desc,或随机。

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

您也可以在第一个参数中传递您自己的字符串:

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

或者,如果您需要多个字段,可以进行多个函数调用。

$this->db->order_by("title", "desc");
$this->db->order_by("name", "asc"); 
// Produces: ORDER BY title DESC, name ASC
于 2012-06-04T15:29:59.280 回答