Skip to content

Instantly share code, notes, and snippets.

@dantoncancella
Created January 18, 2013 12:47
Show Gist options
  • Save dantoncancella/4564350 to your computer and use it in GitHub Desktop.
Save dantoncancella/4564350 to your computer and use it in GitHub Desktop.
Converting the data base return to csv form(for excel)
<?php
/*
* $result should be the return of a function of line "fetch_assoc", returned of a query on the data base
*/
if(!empty($result)) {
$filename = date('Ymd').'.csv';
$cabecalho = array_values(array_keys($result[0]));
$out = '"'.implode('";"', $cabecalho)."\"\r\n";
header("Content-type: text/csv; charset=utf-8");
header("Content-Disposition: attachment; filename=".$filename);
foreach ($result as $key => $value) {
$out .= '"'.implode('";"', $value)."\"\r\n";
}
echo $out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment