2016-01-09 100 views
1

如何從控制器函數中的結果數組中獲取總列字段?獲取codeigniter中表的總列字段?

我得到通過$fields = $query->list_fields()model但如何使用等我從controller得到它。

控制器功能:

$result = $this->admin_model->national_certificate_user_search_print($start_date, $end_date); 
$total = sizeof($result); // getting total rows 

型號功能:

public function national_certificate_user_search_print($start_date, $end_date) { 
     $tablename = 'national_certificate'; 
     $query = $this->db->select('user_name,gurdiant_name,mother_name,prm_village_name,prm_post_office,prm_thana,prm_district,prsnt_address,prsnt_ward,prsnt_post_office,prsnt_thana,prsnt_district') 
         ->from($tablename) 
         ->where("uploaded_date BETWEEN '$start_date' AND '$end_date' ", NULL, FALSE) 
         ->get(); 
     $result = $query->result_array(); 
    // var_dump($query->num_fields()); 
     return $result; 
    } 
} 
+0

它不是直接在控制器中獲取數據的標準。你應該調用一個模型函數。這就是'MVC'。 – urfusion

+0

是我需要調用另一個函數來獲取列字段? –

+1

只是從模型返回'$ query'對象而不是result_array() –

回答

1

試試這個,

控制器:

$result = $this->admin_model->national_certificate_user_search_print($start_date, $end_date); 
$total = $result->list_fields(); //this will work now with Object 

型號:

public function national_certificate_user_search_print($start_date, $end_date) { 
     $tablename = 'national_certificate'; 
     $query = $this->db->select('user_name,gurdiant_name,mother_name,prm_village_name,prm_post_office,prm_thana,prm_district,prsnt_address,prsnt_ward,prsnt_post_office,prsnt_thana,prsnt_district') 
         ->from($tablename) 
         ->where("uploaded_date BETWEEN '$start_date' AND '$end_date' ", NULL, FALSE) 
         ->get(); 
     return $query; 
    } 
} 

在這裏,你只需要從Model返回$query對象,然後你可以用它在Controller使list_fields()功能controller工作。

相關問題