简体   繁体   中英

php custom headers/column heads in csv export

I cannot get custom column heads to show in my csv file. When I use the code below, the column headers appear above every row of data:

header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=$q1b.csv");

$output = fopen("php://output", "w");               
fputcsv($output, array("Date","First","Last","ID"));

foreach($data as $rowList) {
    fputcsv($output, $rowList);
}

fclose($output);  

The csv file looks like below, with the data above the column heads:

"10/11/2018" "Test" "Person" "id info"
"Date" "First" "Last" "ID"

EDIT:::

The following code is $data:

$data =  "\"".$date_formatted."\",\"".$FIRST_NAME."\",\"".$LAST_NAME."\",\"".$Identify_Prospect_UPD."\"\n";
echo $data;

It looks like I have the csv tied to the $data echo, which I don't think I want. Weird!

Turns out it was "operator error" ... translation...me!

I didn't realize I had my header code inside a foreach loop!

Rats!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM