<?PHP$sales=Array( Array( ' Northeast ', ' 2005-01-01 ', ' 2005-02-01 ', 12.54 ),Array( ' Northwest ', ' 2005-01-01 ', ' 2005-02-01 ', 546.33 ),Array( ' Southeast ', ' 2005-01-01 ', ' 2005-02-01 ', 93.26 ),Array( ' Southeast ', ' 2005-01-01 ', ' 2005-02-01 ', 945.21 ),Array( ' All regions ', '--', '--', 1597.34 ),);$output=fopen(' Php://output ', ' W ') or die("Can ' t open php://output");$total= 0;//tell the browser to send a CSV fileHeader("Content-type:application/csv");Header(' Content-disposition:attachment; filename= "Sales.csv");//Output Table HeaderFputcsv ($output,Array(' Region ', ' Start date ', ' End date ', ' Amount '));//outputs each row of data and increments the $totalforeach($sales as $sales _line) {fputcsv ($output,$sales _line); $total+=$sales _line[3];}//Output all data rows sumFputcsv ($output,Array("All Regions", '--', '--',$total));//Close file handlefclose($output) or die("Can ' t close php://output");
Download the CSV file for PHP data