php - PHP FOPEN 函数中的数据未保存到 CSV。改为保存页面 HTML
问题描述
我花了很多时间在这上面,并不断得到相同的结果。我有此代码可将数据保存到 CSV 文件,并在您单击 wordpress 页面模板上的“发送到 CSV”后提示保存文件。它基本上是一个提交按钮和搜索日期字段。如果提交,它将运行此脚本并打开文件以保存为 csv 文件。但是csv文件中的数据本身并不是查询到的数据。相反,它将整个页面模板从页眉到页脚以 < !DOCTYPE html >...< /html > 开头以及数据...我知道查询正在工作。我可以回显数据,它还与所有 HTML 脚本一起显示在 csv 中。我只想要数据而不是整个页面 html。我在这里做错什么了吗?结果应如下所示:
列
乔·史密斯
约翰·多伊杰克
·史密斯
等...
但相反,我得到
< !DOCTYPE html >...HTML 内的所有内容...</ html >
if ($submitbtn == 1) {
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="Attendees.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
// create a file pointer connected to the output stream
$file = fopen('php://output', 'w');
// send the column headers
fputcsv($file, array('Column'));
/* Gather Data for CSV */
$dt = strtotime(str_replace(',', '', $searchdate));
$year = date('Y',$dt);
$month = date('F',$dt);
$day = date('d',$dt);
$args = array(
'post_type' => 'loghistory',
'post_status' => 'publish',
'date_query' => array(
'year' => $year,
'month' => $month,
'day' => $day,
),
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => -1
);
$myQuery = new WP_Query($args);
if ( $myQuery->have_posts() ) :
$count = 0;
while ( $myQuery->have_posts() ) :
$myQuery->the_post();
$thetitle = get_the_title();
$thetitle = str_replace(',',' | ', $thetitle);
fputcsv($file, $thetitle);
$count++;
endwhile;
endif;
wp_reset_postdata();
readfile($filename);
}
解决方案
函数的第二个参数fputcsv()
应该是一个数组,但您传递的是一个字符串。查看以下工作代码示例:
<?php
// output headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="Attendees.csv"');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
// create a file pointer connected to the output stream
$file = fopen('php://output', 'w');
if ( $file ) {
// send the column headers
fputcsv($file, array('Column'));
/* Gather Data for CSV */
// $dt = strtotime(str_replace(',', '', $searchdate));
// $year = date('Y',$dt);
// $month = date('F',$dt);
// $day = date('d',$dt);
// $args = array(
// 'post_type' => 'loghistory',
// 'post_status' => 'publish',
// 'date_query' => array(
// 'year' => $year,
// 'month' => $month,
// 'day' => $day,
// ),
// 'orderby' => 'date',
// 'order' => 'desc',
// 'posts_per_page' => -1
// );
// $myQuery = new WP_Query($args);
// if ( $myQuery->have_posts() ) :
// $count = 0;
// while ( $myQuery->have_posts() ) :
$titles = ['Title1', 'Title2', 'Title3'];
foreach ( $titles as $title ) :
// $myQuery->the_post();
// $thetitle = get_the_title();
// $thetitle = str_replace(',',' | ', $thetitle);
// Notice the second argument which must be an array
fputcsv($file, [$title]);
// $count++;
endforeach;
// endwhile;
// endif;
// wp_reset_postdata();
}
fclose($file);
推荐阅读
- gitlab - 更新 gitlab 文件权限
- c# - 从另一个表中检索数据并插入另一个表
- javascript - 服务中的异步调用是否会阻止 angularjs 中的组件加载?
- excel - 如何使用 2 个不同的变量合并重复值
- r - 做散点图,基于列的阶乘值的数值分布
- database - Laravel 8 框架中的自联接
- swift - 如何引用现有的 HStack、VStack 和 ZStack 实例?
- python - 对于 UNEVEN 结构化数据,使用 Pandas '比较和替换'值
- jmeter - 250 个用户在 jmeter 中达到了大约 60k 的总体请求
- mysql - Laravel sql 插入无法通过 DB::unprepared 正常工作