2016-10-01 156 views
1

我正在開發一個項目,該項目需要根據多個條件從MYSQL DB導出數據。我指thisCodeIgniter:將數據從數據庫導出到Excel中並下載

這是我的源代碼:

public function exportExcelData($records) 
{ 
    $heading = false; 
     if (!empty($records)) 
      foreach ($records as $row) { 
       if (!$heading) { 
        // display field/column names as a first row 
        echo implode("\t", array_keys($row)) . "\n"; 
        $heading = true; 
       } 
       echo implode("\t", ($row)) . "\n"; 
      } 
} 

public function fetchDataFromTable() 
{ 
    $query =$this->db->get('one_piece_characters'); // fetch Data from table 
    $allData = $query->result_array(); // this will return all data into array 
    $dataToExports = []; 
    foreach ($allData as $data) { 
     $arrangeData['Charater Name'] = $data['name']; 
     $arrangeData['Charater Profile'] = $data['profile']; 
     $arrangeData['Charater Desc'] = $data['description']; 
     $dataToExports[] = $arrangeData; 
    } 
    // set header 
    $filename = "dataToExport.xls"; 
       header("Content-Type: application/vnd.ms-excel"); 
       header("Content-Disposition: attachment; filename=\"$filename\""); 
    $this->exportExcelData($dataToExports); 
} 

如果我使用它沒有任何where子句,它給整個表。

如果我只使用single where子句,它的效果很好。

伯,如果我使用多個條件。它給了我一張空白的Excel表格。

有沒有人有任何想法如何使它與多個條件工作?

+0

你肯定有是在你的數據庫適合你'多的任何數據conditions'? –

+0

顯示你的位置? –

+0

用你的多個條件試試echo'$ this-> db-> last_query()',並在'database'或'phpmyadmin'中執行相同的查詢並檢查是否得到任何結果 – Zeeshan

回答

3

嘗試使用在你的模型文件驗證碼:以CSV

function createcsv(){ 
     $this->load->dbutil(); 
     $this->load->helper('file'); 
     $this->load->helper('download'); 
     $delimiter = ","; 
     $newline = "\r\n"; 
     $filename = "filename.csv"; 
     $query = "SELECT * FROM YourTable"; //USE HERE YOUR QUERY 
     $result = $this->db->query($query); 
     $data = $this->dbutil->csv_from_result($result, $delimiter, $newline); 
     force_download($filename, $data); 

    } 
+0

它爲我工作..感謝 –

0

// ecport contect名單,

public function cnt_explode(){ 

$csvData[] =array( "Name", "Email Id","Mobile No.","Event", "City","location","No of guest","Event Date","Budget", 
        "Venue Type","Food","Drink","Description","Date"); 
$data = $this->contact_model->get_contact(NULL); 

foreach($data as $cnt){ 
$csvData[]=array(
     $cnt->name ,$cnt->email, $cnt->mobile_no, $cnt->event, $cnt->city, $cnt->location, $cnt->no_guest,$cnt->event_date,$cnt->budget, $cnt->venue_type, 
     $cnt->food, $cnt->drink, $cnt->description,$cnt->created_on, 
    ); 
} 

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Disposition: attachment;filename=venuexpo_".time().".csv"); 
header("Content-Transfer-Encoding: binary"); 
$df = fopen("php://output", 'w'); 
array_walk($csvData, function($row) use ($df) { 
    fputcsv($df, $row); 
}); 
fclose($df); 
} 

///------------ downlode csv done -------------- 
相關問題