簡體   English   中英

通過codeigniter中的查詢計算具有詳細信息的行數

[英]count number of rows with detail through query in codeigniter

我正在codeigniter建立一個旅行社網站。 我從我的數據庫中獲取非洲的目的地並在頁面上顯示。 這是我的控制器代碼:

public function africa() {
    $data['africa']= $this->Travel->africa_des();
    $this->load->view('africa', $data);
} 

我的型號:

function africa_des() {
    $this->db->distinct();
    $this->db->select();
    $this->db->from('travels_detail');
    $this->db->like('region', 'africa');
    $this->db->limit(5);
    $query= $this->db->get();

    return $query->result_array();
}

我想獲得我在數據庫中擁有的每個目的地的總航班數。 例如,我有25個阿布賈航班,所以我想從阿布賈目的地獲得這個號碼。

任何人都有任何想法?

試試這個吧

public function africa_desc(){
    $this->db->distinct();
    $this->db->select('*');
    $this->db->from('travels_detail');
    $this->db->where('region', 'africa');
    $this->db->limit(5);
    $query= $this->db->get();
    return $query->result_array();
}

當你使用$this->db->distinct() ,必須使用$this->db->group_by('column_name')請參閱此答案https://stackoverflow.com/a/19509869/6098616 在您的情況下,我將假設destination是您的列名稱。

 public function africa_desc(){ $this->db->select('*'); $this->db->distinct(); $this->db->group_by('destination'); $this->db->from('travels_detail'); $this->db->where('region', 'africa'); $this->db->limit(5); $query= $this->db->get(); return $query->result_array(); } 

希望這會有所幫助。

試試這個計算航班

public function africa_desc(){    
$this->db->select('*');
$this->db->distinct();
$this->db->group_by('destination');
$this->db->from('travels_detail');
$this->db->where('region', 'africa');
$this->db->limit(5);
$query= $this->db->get();
return $query->num_rows();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM