我需要将MySQL查询的结果导出到codeigniter中的csv文件中。
这是模型:
public function export_csv()
{
$this->load->dbutil();
$this->load->helper('file');
$this->load->helper('download');
$delimiter = ",";
$newline = "\r\n";
$file_name = 'BVN_REPORTS'.date("Y-m-d h-i-s").'.csv';
$query = 'SELECT account_name as "Account Name",
api_account_name as "Verified Name",
account_num "Account Number",
bvn "Bank Verification Number (BVN)", bank_name as "BANK NAME"
from ew_employees where bvn is not null
ORDER BY bank_name ';
$result = $this->db->query($query);
$data = $this->dbutil->csv_from_result($result, $delimiter, $newline);
if(force_download($filename, $data)){
echo 'Done';
}
else {echo 'Not Done';}
}
这是控制器:
public function get_csv()
{
$this->load->model('employees_model');
$this->employees_model->export_csv();
}
结果总是“未完成”......如何强制下载查询结果的csv。谢谢
答案 0 :(得分:1)
根据问题标题,让我回答这个问题。一般来说,访问这里时可能会有所帮助。 将数据导出到csv时我通常会做什么。
constructor() {
super();
this.ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
modalVisible: false,
todoText:'',
todoListArray:['foo 1','foo 2'],
};
}
答案 1 :(得分:0)
控制器
$result = $this->registration_model->exportToCsv();
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=\"application".".csv\"");
header("Pragma: no-cache");
header("Expires: 0");
$handle = fopen('php://output', 'w');
fputcsv($handle, array('Sr No.', 'Apply for', 'Name', 'mobile', 'email_id', 'work_experinece', 'pan', 'refrence', 'pro_img1', 'pro_img2', 'created_at'));
$i = 1;
foreach ($result as $data) {
fputcsv($handle, array($i, $data["apply_for"], $data["full_name"], $data["mobile"], $data["email_id"], $data["work_experinece"], $data["pan"], $data["refrence"], $data["pro_img1"], $data["pro_img2"], $data["created_at"]));
$i++;
}
fclose($handle);
exit;
模型
$this->db->select("apply_for,full_name,mobile,address,email_id,work_experinece,pan,refrence,pro_img1,pro_img2,created_at");
$this->db->order_by("app_id", "DESC");
$this->db->from("tablename");
$query = $this->db->get();
return $query->result_array();
答案 2 :(得分:0)
我在 Abdul Manan 的回答中添加了一行来处理 UTF-8 字符:
foreach ($array as $array) {
fputs($handle, $bom =( chr(0xEF) . chr(0xBB) . chr(0xBF) ));
fputcsv($handle, $array);
}