通过codeigniter生成管道分隔文件
generate pipe delimited file through codeigniter
我正在使用下面的 csv 导出,但我想导出为管道分隔的输出文本文件格式。
我的代码使用 PHP 的 fputcsv 函数生成了一个 txt 文件。
对于分隔符,我尝试使用“|”。
这个我的代码:
function to_CSV($table) {
$file_csv = "file_csv.csv";
$fp = fopen('php://output', 'w');
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='htmltable' AND TABLE_NAME='$table'";
$result = mysqli_query(db_connect(),$query);
while ($row = mysqli_fetch_row($result)) {
$header[] = $row[0];
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$file_csv);
fputcsv($fp, $header);
$query ="SELECT * from $table";
$result = mysqli_query(db_connect(),$query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit;
}
fclose($fp);
$contents = file_get_contents($file_csv);
$contents = str_replace(",", "|", $contents);
file_put_contents($file_csv, $contents);
如何在codeigniter中实现。请帮帮我。
非常感谢。
according to the docs fputcsv
format line as CSV and write to file pointer, it has a third parameter which expects a delimiter - take a look at
https://www.php.net/manual/en/function.fputcsv
在你的情况下,这意味着
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row, '|');
}
然而,主要问题是 - 如果您使用 Codeigniter 作为基础框架 - 为什么不使用模型原理以及所提供的查询构建器? - 它会让你的生活更轻松。
You can find more informations in their very well written documentation. Take a look at https://codeigniter.com/user_guide/general/models.html?highlight=model
我正在使用下面的 csv 导出,但我想导出为管道分隔的输出文本文件格式。 我的代码使用 PHP 的 fputcsv 函数生成了一个 txt 文件。 对于分隔符,我尝试使用“|”。
这个我的代码:
function to_CSV($table) {
$file_csv = "file_csv.csv";
$fp = fopen('php://output', 'w');
$query = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='htmltable' AND TABLE_NAME='$table'";
$result = mysqli_query(db_connect(),$query);
while ($row = mysqli_fetch_row($result)) {
$header[] = $row[0];
}
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$file_csv);
fputcsv($fp, $header);
$query ="SELECT * from $table";
$result = mysqli_query(db_connect(),$query);
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row);
}
exit;
}
fclose($fp);
$contents = file_get_contents($file_csv);
$contents = str_replace(",", "|", $contents);
file_put_contents($file_csv, $contents);
如何在codeigniter中实现。请帮帮我。 非常感谢。
according to the docs
fputcsv
format line as CSV and write to file pointer, it has a third parameter which expects a delimiter - take a look at https://www.php.net/manual/en/function.fputcsv
在你的情况下,这意味着
while($row = mysqli_fetch_row($result)) {
fputcsv($fp, $row, '|');
}
然而,主要问题是 - 如果您使用 Codeigniter 作为基础框架 - 为什么不使用模型原理以及所提供的查询构建器? - 它会让你的生活更轻松。
You can find more informations in their very well written documentation. Take a look at https://codeigniter.com/user_guide/general/models.html?highlight=model