php - 通过php mysql导出csv中的多行

标签 php mysql

这些代码可以正常显示两行和多列数据, 但我想在附加行中显示附加文本标题

当前代码显示如下:

数据编号 |用户名 |办公室
1 | 1 |做

我想要类似的东西

库存报告
日期
数据编号 |用户名 |办公室
1 | 1 |做

<?php if (isset($_POST['exp'])) {   
$wpdb->show_errors(); 
global $wpdb;

// Grab any post values you sent with your submit function
$DownloadReportFrom = "ReportDateFrom";
$DownloadReportTo = "ReportDateFrom";

// Build your query                     
$MyQuery = $wpdb->get_results('SELECT * FROM wp_rpci_rpci');

$result = $wpdb->get_results('SELECT sum(balancepercard) as result_value     FROM wp_rpci_rpci');
$total = $result[0]->result_value;  


// Process report request
if (! $MyQuery) {
$Error = $wpdb->print_error();
die("The following error was found: $Error");
} else {
// Prepare our csv download

// Set header row values
$csv_fields=array();
$csv_fields[] = 'DATA ID';
$csv_fields[] = 'USER ID';
$csv_fields[] = 'OFFICE';
$csv_fields[] = 'ARTICLE';
$csv_fields[] = 'DESCRIPTION';
$csv_fields[] = 'STOCK NUMBER';
$csv_fields[] = 'UNIT OF MEASURE';
$csv_fields[] = 'UNIT VALUE';
$csv_fields[] = 'BALANCE PER CARD';
$csv_fields[] = 'ONHAND PER COUNT';
$csv_fields[] = 'S/O QUANTITY';
$csv_fields[] = 'S/O VALUE';
$csv_fields[] = 'REMARKS';
$csv_fields[] = '';
$csv_fields[] = 'TOTAL BALANCE: '.$total.'';


$output_filename = 'MyReport_' . $DownloadReportFrom .'-'. $DownloadReportTo  . '.csv';
$output_handle = @fopen( 'php://output', 'w' );

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=' . $output_filename );
header( 'Expires: 0' );
header( 'Pragma: public' ); 

// Insert header row
fputcsv( $output_handle, $csv_fields );

// Parse results to csv format
foreach ($MyQuery as $Result) {
$leadArray = (array) $Result; // Cast the Object to an array
// Add row to file
fputcsv( $output_handle, $leadArray );
}

// Close output file stream
fclose( $output_handle ); 

die();
}
  }
?>

最佳答案

更新您的代码:

// Insert header row(S)
fputcsv( $output_handle, array('REPORT IN INVENTORY') );
fputcsv( $output_handle, array('DATE') );
fputcsv( $output_handle, $csv_fields );

这应该在顶部添加您想要的 2 个字段

关于php - 通过php mysql导出csv中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35496682/

相关文章:

javascript - 根据选择显示表格/表格

php - 在 Laravel 中限制单词而不是字符

MySQL 从 2 个或多个表中进行选择,其中的 where 子句具有表 A 中的条件位于表 B 中

mysql - 如何查找 mySQL DB 中没有重复的行?

php - Load Data Infile 出现无缓冲查询错误

php - 使用 preg_replace 转换缩进(无回调)

javascript - 如何从 vimeo url/embed 获取视频 ID?

mysql - 分组时如何与条件求和

python - MySQL 用一条语句执行多个相似的选择查询

mysql - 考虑到一个条件仅限于几个值,可以有效地进行过滤