add_action('admin_post_learning_profile_csv_download', 'learning_profile_csv_download');
function learning_profile_csv_download(){
$spreadsheet = new Spreadsheet();
$Excel_writer = new Xls($spreadsheet);
$spreadsheet->setActiveSheetIndex(0);
$activeSheet = $spreadsheet->getActiveSheet();
$activeSheet->setCellValue('A1' , 'New file content')->getStyle('A1')->getFont()->setBold(true);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'. $filename .'.csv"');
header('Cache-Control: max-age=0');
$Excel_writer->save('php://output');
$user_id = get_current_user_id();
echo $user_id;
echo '<br>';
$learning_plan = get_user_meta( get_current_user_id(), 'study_guide', true);
if( empty( $learning_plan ) ) {
echo 'No Resources Currently Recommended.';
} else {
foreach( $learning_plan as $item ) {
$resource_comps = get_the_terms( $item, 'ivpt_cc' );
echo get_the_title($item);
foreach( $resource_comps as $comp ){
echo $comp->name;
echo'<br>';
}
}
}
}
这是我的代码,我想将学习计划数据下载为CSV格式,分为两列,一次是标题,另一列是comp-> name。那么,如何在foreach循环中为学习计划设置列和行呢?
答案 0 :(得分:1)
对您的foreach循环进行了一些修改
请注意,您不能并行回显任何html,即使用<br>
标签回显...
希望有帮助
$title = get_the_title($item);
$csv = "";
foreach ( $resource_comps as $comp ){
$csv .= "'" . $title . "','" . $comp->name . "\r\n";
}
header('Content-type: text/csv');
echo $csv;