2012-06-04 22 views
5

Zastanawiałem się, czy ktoś może mi w czymś pomóc.Codeigniter - Zamawianie aktywnego rekordu alfabetycznie

Mam trochę ajax, który wywołuje funkcję w moim modelu.

Ale wydaje się, że nie mogę zamówić wyjścia przez "model".

Poniżej funkcja im kłopoty z

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')'? –

Odpowiedz

18

From the documentation,

$ this-> db-> order_by();

Umożliwia ustawienie klauzuli ORDER BY. Pierwszy parametr zawiera nazwę kolumny, którą chcesz zamówić. Drugi parametr pozwala ustawić kierunek wyniku. Dostępne są opcje asc lub desc lub losowo.

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

Można również przekazać swój własny ciąg w pierwszym parametrze:

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

lub wielokrotne wywołania funkcji może być dokonane, jeżeli trzeba wiele pól.

$this->db->order_by("title", "desc"); 
$this->db->order_by("name", "asc"); 
// Produces: ORDER BY title DESC, name ASC 
Powiązane problemy