导出php for-loop表为csv:工作,但打印页html和重复的条目在csv文件



我试图为woocommerce订单创建一个自定义导出到CSV。代码到目前为止运行正常,但是打印出来的csv文件看起来有些奇怪。整个页面html打印在csv文件中,所需的循环/表只出现在底部。查看表,还可以看到条目是重复的——类似于订单ID 1558连续出现两次,所有条目都是重复的。我花了几个小时在这里寻找解决方案,特别是在csv文件中删除html。大多数解决方案建议我将头函数放在顶部,其他解决方案建议我从代码中删除html。我已经尝试了所有这些,但似乎仍然缺少一些东西。我使用元素,并在前端创建了一个html按钮。我现在试图使用这个按钮来调用csv文件下载。请帮助。

function orderscsvdownload(){
if (isset( $_POST['export'] ) ) {// when button on the page is clicked
$fromdate = $_POST['fromDate'];
$todate = $_POST['toDate'];
$args = array(
'post_type' => array( 'shop_order' ),
'posts_per_page' => 30,
//'paged' => $paged,

'limit' => -1, 
);
$tablecontents= wc_get_orders($args);
$fileName = "phpzag_export_".date('Ymd') . ".csv";
$file = fopen('php://output', 'w');
$header = array("Id", "Name", "Item", "Value", "Date");
fputcsv($file, $header); 
foreach ($tablecontents as $content) {
$orderData = array();
$orderData[] = $content->get_id();
$orderData[] = $content->get_total();
$orderData[] = $content->get_status();
$orderData[] = $content->get_billing_first_name();
$orderData[] = $content->get_date_created();
// ob_end_clean(); 
fputcsv($file, $orderData);
fputcsv($file, $orderData);
}
fclose($file);
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/csv;");   
exit;

}

}

add_shortcode('csvdownload','orderscsvdownload');// i had to add this shortcode on the page for the download to work. Sort of calls this function on the page for the download to work.

20多个小时后生命又好了!我把密码打开了。这不是一个专业的解决方案,但让我感到自豪,并为像我这样的新手工作,他们正在努力做ajax调用,jQuery,甚至不知道如何从wpdb检索一些东西。欢迎你对答案进行改进。

function orderscsvdownload() {
$args = array(
'post_type' => array( 'shop_order' ),
'posts_per_page' => 1000,
//'paged' => $paged,
'post_status' => array( 'wc-processing','wc-eviction-notice'),
'limit' => -1, 
);  
//ob_get_clean();

if (isset( $_POST['export'] ) ) {// when button on the page is clicked
$fromdate = $_POST['fromDate'];
$todate = $_POST['toDate'];
ob_get_clean(); 
$tablecontents= wc_get_orders($args);
ob_get_clean();
$fileName = "Homa_".date('Y-m-d') . ".csv";
ob_get_clean();
header("Content-Description: File Transfer");   
ob_get_clean();
header("Content-Disposition: attachment; filename=$fileName");  
ob_get_clean();
header("Content-Type: application/csv;"); 
ob_get_clean();
$file = fopen('php://output', 'w');

$header= array("Idy","Name","Item","Value","Date");

//ob_end_clean();   // works with here

fputcsv($file, $header); 

foreach ($tablecontents as $content) {
//ob_end_clean();
$orderData = array();
$orderData[] = $content->get_id();
$orderData[] = $content->get_total();
$orderData[] = $content->get_status();
$orderData[] = $content->get_billing_first_name();
$orderData[] = $content->get_date_created();

// ob_get_clean();
fputcsv($file, $orderData);

// fputcsv($file, $orderData );
}

fclose($file);
exit;
}

$html='<div>

<form method="post">

From: <input type="date" name="fromDate"/>
To: <input type="date" name="toDate"/>

<input type="submit" name="export" value="Export to CSV" class="btn btn-default" />
</form>
</div>';
// ob_get_clean($html);
return $html;   
}
add_shortcode('csvdownload','orderscsvdownload');// 

我必须在页面上添加这个短代码才能下载。在页面上调用这个函数来下载

最新更新