我对使用 codeigniter 从数据库导出数据感到震惊。非常感谢您的帮助。
这是我针对此问题的查看代码。
<?php echo form_open(base_url('admin/consignment1/test_con'),
$hiddenFields, 'class="form-horizontal"'); ?>
<td><input type="submit" name="id" value="<?php echo $row['Con_No']; ?>"
class="btn btn-info pull-left"> </td>
Controller
public function test_con(){
$var= $this->input->post('id');
$data['get_con_by'] = $this->consignment_model->get_con_by($var);
$data['view']='admin/consignment1/con_table';
$this->load->view('admin/layout', $data);
}
型号
public function get_con_by($var){
$this->db->from('consignment');
$this->db->where('Con_No',$var);
$query=$this->db->get();
return $result = $query->result_array();
}
在我的 View 上显示数据没有问题。当我尝试使用其他函数从模型导出数据时,我得到的 CSV 包含空数据。这是我在同一型号 Controller 上的功能。
public function exportCSV(){
// file name
$filename = 'Con_'.date('Ymd').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
// get data
$usersData = $this->consignment_model->get_con_by($var);
// file creation
$file = fopen('php://output', 'w');
$header = array("ste","Name","Gender","Email");
fputcsv($file, $header);
foreach ($usersData as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
我有另一个模型
public function get_all_pins(){
$query = $this->db->get('consignment');
return $result = $query->result_array();
}
导出到 CSV 时没有问题
两种场景之间的区别是
$usersData = $this->consignment_model->get_con_by($var); // Not working
$usersData = $this->consignment_model->get_all_pins(); // working one.
你能帮我解决这个问题吗?
最佳答案
要获取查询的答案,请 checkin 使用生成的查询
$this->db->last_query()
checkin 生成的查询。和你期待的一样还是有什么问题?您可以在 phpmyadmin 中交叉验证同一查询的输出。
我在这里预见的唯一问题是生成的查询不会返回任何数据/输出,或者在执行查询时可能会出现错误。第二个是不太预期的。但是以这种方式进行相同的调试将帮助您解决实际问题。
关于php - Codeigniter - 导出到 CSV 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47800209/