Php CodeIgniter:将数据库中的数据导出到excel并下载
我正在做一个项目,需要根据多种条件从MYSQL数据库导出数据。我指的是: 这是我的源代码:Php CodeIgniter:将数据库中的数据导出到excel并下载,php,mysql,excel,codeigniter,export-to-excel,Php,Mysql,Excel,Codeigniter,Export To Excel,我正在做一个项目,需要根据多种条件从MYSQL数据库导出数据。我指的是: 这是我的源代码: public function exportExcelData($records) { $heading = false; if (!empty($records)) foreach ($records as $row) { if (!$heading) { // display field
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子句,它将给出整个表
如果我只使用一个where子句,它会很好地工作
但是,如果我使用多个where条件。它给了我一张空白的excel表格
是否有人知道如何使其在多个where条件下工作?尝试在模型文件中使用以下代码:
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);
}
尝试在模型文件上使用以下代码:
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);
}
//csv格式的ecport内容列表
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 --------------
//csv格式的ecport内容列表
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 --------------
你确定数据库中有符合你的
多个条件的数据吗?显示你的where条件吗?试试echo$this->db->last\u query()
使用您的多个条件,并在数据库中执行相同的查询
或phpmyadmin
&检查您是否得到任何查询results@Zeeshan查询如下所示:从deptid=5和deptname=“motor”
的dept中选择*并且不返回任何数据。它给出了一张空白的Excel表。您是否尝试过直接在数据库中运行相同的查询,如果您能得到任何查询结果,是否确定数据库中有符合您的多个条件的数据?显示您的where条件?尝试echo$this->db->last\u query()
使用您的多个条件,并在数据库中执行相同的查询
或phpmyadmin
&检查您是否得到任何查询results@Zeeshan查询如下所示:从deptid=5和deptname=“motor”
的dept中选择*并且不返回任何数据。它会给出一张空白的Excel表。您是否尝试过直接在数据库中运行相同的查询,以及是否可以找到查询结果