2016-04-05 19 views
2

我試圖將數據導出爲CSV。我只想在CodeIgniter中有一些CSV字段,但它說「你必須提交一個有效的結果對象」。我在我的代碼中做錯了什麼?如何在CodeIgniter中僅添加來自查詢的一些列

這是我試過的代碼:

$projectdetails = $this->db->query('select hs_hr_employee.emp_number,hs_hr_employee.emp_firstname,hs_hr_employee.emp_middle_name,hs_hr_employee.emp_lastname,ohrm_customer.customer_id,ohrm_customer.name as customername,ohrm_project.project_id,ohrm_project.customer_id,ohrm_project.name as projectname, 
       ohrm_project_admin.*, hs_hr_employee.* 
       from hs_hr_employee,ohrm_customer,ohrm_project, 
       ohrm_project_admin where 
       ohrm_project_admin.project_id=ohrm_project.project_id and 
       ohrm_project.customer_id=ohrm_customer.customer_id and ohrm_project_admin.project_id=ohrm_project.project_id and ohrm_project_admin.emp_number=hs_hr_employee.emp_number 
       '); 


    $this->load->dbutil(); 
    $this->load->helper('download'); 
    $delimiter = ","; 
    $newline = "\r\n"; 
    foreach($projectdetails->result() as $projectdetails12) 
    { 
     $projectname=$projectdetails12->projectname; 
     $customername=$projectdetails12->customername; 
     $projectadmin=$projectdetails12->emp_firstname.$projectdetails12->emp_middle_name.$projectdetails12->emp_lastname; 
     $downloadprojectdetails=array('projectname'=>$projectname,'customername'=>$customername,'projectadmin'=>$projectadmin); 

     $filename = 'projectdetails.csv'; 
     $data = $this->dbutil->csv_from_result($downloadprojectdetails, $delimiter, $newline); 
     force_download($filename, $data); 
    } 

output using print_r 
Array ([0] => stdClass Object ([emp_number] => 3 [emp_firstname] => Suresh [emp_middle_name] => [emp_lastname] => Katiki [customer_id] => 1 [customername] => suresh [project_id] => 2 [projectname] => fff) [1] => stdClass Object ([emp_number] => 2 [emp_firstname] => praveen [emp_middle_name] => [emp_lastname] => reddy [customer_id] => 2 [customername] => praveen [project_id] => 3 [projectname] => ota) [2] => stdClass Object ([emp_number] => 1 [emp_firstname] => chandra [emp_middle_name] => [emp_lastname] => kanth [customer_id] => 3 [customername] => chandra [project_id] => 1 [projectname] => orangehrm) [3] => stdClass Object ([emp_number] => 4 [emp_firstname] => aashish [emp_middle_name] => [emp_lastname] => madiri [customer_id] => 4 [customername] => aashish [project_id] => 4 [projectname] => test)) 
+0

顯示的代碼'$這個 - > dbutil-> csv_from_result()' – Vinie

回答

0

$this->dbutil->csv_from_result僅需要查詢對象不會導致對象。 這裏你必須通過$this->dbutil->csv_from_result($projectdetails,$delimiter, $newline);沒有任何數組或結果對象

試試下面的代碼

$projectdetails = $this->db->query('select ohrm_project.name as projectname, 
concat(hs_hr_employee.emp_firstname," ",hs_hr_employee.emp_middle_name," ", hs_hr_employee.emp_lastname) as projectadmin, 
ohrm_customer.name as customername 
      from hs_hr_employee,ohrm_customer,ohrm_project, 
      ohrm_project_admin where 
      ohrm_project_admin.project_id=ohrm_project.project_id and 
      ohrm_project.customer_id=ohrm_customer.customer_id and ohrm_project_admin.project_id=ohrm_project.project_id and ohrm_project_admin.emp_number=hs_hr_employee.emp_number 
      '); 


$this->load->dbutil(); 
$this->load->helper('download'); 
$this->load->helper('file'); 
$delimiter = ","; 
$newline = "\r\n"; 
$filename = 'projectdetails.csv'; 
if($projectdetails->num_rows() >0) 
{ 
    $data = $this->dbutil->csv_from_result($projectdetails, $delimiter, $newline); 
    force_download($filename, $data); 
} 
+0

不工作的話,必須提交一個有效的結果對象 – user3396954

+0

是你的查詢返回任何東西 – Vinie

+0

其顯示此錯誤你必須提交一個有效的結果對象 – user3396954

相關問題