[英]MYSQL PHP exports csv to one column! Hove to export in to separated columns?
I need help to find why is exporting this way and how to fix it?我需要帮助来找出为什么以这种方式导出以及如何解决它?
my code exportes this way:我的代码以这种方式导出:
The goal is to get this:目标是得到这个:
code:代码:
include 'settings.php';
if(isset($_POST["export"]))
{
$filename = "adat_" . date('Y-m-d') . ".csv";
$delimiter = ",";
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '";');
$output = fopen("php://output", "w");
fputcsv($output, array('barcode', 'name', 'status', 'log_in', 'log_out'), $delimiter);
$query =
"SELECT userlog.barcode, workers.name, workers.status, userlog.log_in, userlog.log_out
FROM userlog
INNER JOIN workers
ON workers.barcode=userlog.barcode";
$result = mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($result))
{
fputcsv($output, $row);
}
fclose($output);
}
?>
Bro, code is ok export is ok, but u need to set "," separator to google sheet or excel.兄弟,代码没问题导出没问题,但你需要将“,”分隔符设置为谷歌表或excel。
if you use google sheet, don't copy text from csv, do import, and press auto detect separator.如果您使用谷歌表,不要从 csv 复制文本,请导入,然后按自动检测分隔符。
This is the solution for the problem!这就是问题的解决方案! Thx for the help!
谢谢你的帮助!
include 'settings.php';
if(isset($_POST["export"]))
{
$sql_query = "SELECT userlog.barcode, workers.name, workers.status, userlog.log_in, userlog.log_out
FROM userlog
INNER JOIN workers
ON workers.barcode=userlog.barcode";
$resultset = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
$developer_records = array();
while( $rows = mysqli_fetch_assoc($resultset) ) {
$developer_records[] = $rows;
}
$filename = "phpzag_data_export_".date('Ymd') . ".xls";
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=\"$filename\"");
$show_coloumn = false;
if(!empty($developer_records)) {
foreach($developer_records as $record) {
if(!$show_coloumn) {
// display field/column names in first row
echo implode("\t", array_keys($record)) . "\n";
$show_coloumn = true;
}
echo implode("\t", array_values($record)) . "\n";
}
}
exit;
}
?> ```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.