mysql - Export database results to CSV with PHP -
i've trauled through site , found tonnes of threads same thing none complete novice me dont understand answers properly.
i have selected data database , displayed in table on webpage. need link below table export data csv file.
one of answers have found on site is: create csv file user in php
however answer doesnt make sense me , dont know code.
here code answer:
header("content-type: text/csv"); header("content-disposition: attachment; filename=file.csv"); header("pragma: no-cache"); header("expires: 0"); $array = array( array("data11", "data12", "data13"), array("data21", "data22", "data23"), array("data31", "data32", "data23")); outputcsv($array); function outputcsv($data) { $outstream = fopen("php://output", "w"); function __outputcsv(&$vals, $key, $filehandler) { fputcsv($filehandler, $vals); // add parameters if want } array_walk($data, "__outputcsv", $outstream); fclose($outstream); }
i have many questions need create excel file first? if need blank?
what this?:
$array = array( array("data11", "data12", "data13"), array("data21", "data22", "data23"), array("data31", "data32", "data23"));
is supposed data? how change suit data have?
why call function before function defined?
what these variables in function ($vals, $key, $filehandler) , created?
how use code because many people seem think perfect.
i need start finish complete php novice
ive worked on 1 of answers below , have code
$i=0; $csv=""; ($a=0; $a<=$count; $a++) { $i++; $csv.=preg_replace("/\n/",'',preg_replace("/,/",';',$serveys[$a]['feedbackname'])).",". preg_replace("/\n/",'',preg_replace("/,/",';',$serveys[$a]['branchname'])); $csv.="\n"; } if ($i>0) { header("pragma: public"); header("expires: 0"); header("cache-control: must-revalidate, post-check=0, pre-check=0"); header("cache-control: private",false); header("content-type: application/octet-stream"); header("content-disposition: attachment; filename=\"table.csv\";" ); header("content-transfer-encoding: binary"); echo $csv; } else { return "nothing download!"; }
when run page though there no change, data still displayed on page should be. no csv file created , no errors occurr
the code created new file object, outputs standard output, offered user download because of headers sent before.
that data; array of associative arrays. how fill you.
the call function before defined because php first parses file @ top level completely, function defined when called. not apply functions inside if statements.
the values function provided standard function fputcsv.
you use code presented.
Comments
Post a Comment