I am trying to create a CSV file in my php script. It creates the file but each time it inserts an empty row at the end.
My code is below:
$csv_output .= "Header 0,";
$csv_output .= "Header 1,";
$csv_output .= "Header 2,";
$csv_output .= "Header 3,";
$csv_output .= "Header 4,";
$csv_output .= "\r\n";
// db query...
foreach($rows as $row):
if($row["value_3"] == 10)
{
$x = 'low';
}
else if($row["value_3"] == 100)
{
$x = 'high';
}
$csv_output .= "".$row["value_0"].",";
$csv_output .= "".$row["value_1"].",";
$csv_output .= "".$row["value_2"].",";
$csv_output .= "".$x.",";
$csv_output .= "Constant Always Value Goes here (not in db),";
$csv_output .= "\r\n";
endforeach;
$filename = "csv_".date("d-m-Y_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
No matter what my database query is or how many rows the csv contains there is always an empty row at the end. Is there anything causing this?
"") before each value..... it's pointless and meaningless overheadprint $csv_output;line\r\nas an EOL character, that may work on your windows box, but all *NIX like systems use\n. Thankfully, PHP has a magic constantPHP_EOLthat depends on the platform