2012-06-04 64 views
5

我想知道是否有人可以幫我解決問題。Codeigniter - 按字母順序排列有效記錄

我有一點在我的模型中調用函數的ajax。

但我似乎無法按'模型'排序輸出。

下方具有功能IM麻煩

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; 
    } 
} 
+2

'$ this-> db-> order_by('model')'? –

回答

18

From the documentation

$這 - > 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