gpt4 book ai didi

php - wordpress 导出 csv 在管理菜单页面中不起作用

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:12 25 4
gpt4 key购买 nike

我在自定义主题的 functions.php 文件中创建了一个管理菜单页面,我想在单击该菜单时以 csv 格式导出数据。

我已经为此编写了以下代码,但是当我单击该菜单时,我在 excel 中得到了垃圾格式的结果。

enter image description here

以下是我在自定义主题的 functions.php 文件中编写的代码

1) 添加自定义管理菜单页面

<?php
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
add_menu_page('Export Payment History', 'Export Payment History', 'manage_options', 'export-history', 'export_payment_history','dashicons-image-rotate-right' );
}
?>

2) 回调函数

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
0 => 'ID',
1 => 'TRANSACTION ID',
2 => 'EMAIL',
3 => 'PAYMENT DATE',
4 => 'TOTAL AMOUNT',

);


$i = 1;
foreach( $results as $result ) {

$item_unserialize = unserialize($result->item_data);

$user_info = get_userdata($result->user_id);
$user_email = $user_info->user_email;

$row = array();
$row[0] = $i;
$row[1] = $result->txn_id;
$row[2] = $user_email;
$row[3] = $result->payment_date;
$row[4] = $item_unserialize['payment_total']['payment_gross'];

$data_rows[] = $row;

$i++;
}

$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );



fputcsv($fh, $header_row);

foreach ($data_rows as $data_row) {
fputcsv($fh, $data_row);
}

fclose( $fh );
exit();

}

我在 excel 表中得到了垃圾值,没有得到任何数据请帮助我。

我得到如下输出 enter image description here

最佳答案

首先谢谢大家,我已经自己解决了

我刚刚添加了 stream_get_contents

完整代码..

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
0 => 'ID',
1 => 'TRANSACTION ID',
2 => 'EMAIL',
3 => 'PAYMENT DATE',
4 => 'TOTAL AMOUNT',
);


$i = 1;
foreach( $results as $result ) {

$item_unserialize = unserialize($result->item_data);

$user_info = get_userdata($result->user_id);
$user_email = $user_info->user_email;

$row = array();
$row[0] = $i;
$row[1] = $result->txn_id;
$row[2] = $user_email;
$row[3] = $result->payment_date;
$row[4] = $item_unserialize['payment_total']['payment_gross'];

$data_rows[] = $row;

$i++;
}

ob_end_clean();
$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );

fputcsv($fh, $header_row);

foreach ($data_rows as $data_row) {
fputcsv($fh, $data_row);
}

$csvFile = stream_get_contents($fh);
fclose($fh);
die();
}

关于php - wordpress 导出 csv 在管理菜单页面中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47428252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com