Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active December 25, 2015 04:39
Show Gist options
  • Save Kcko/6918548 to your computer and use it in GitHub Desktop.
Save Kcko/6918548 to your computer and use it in GitHub Desktop.
PHP: CSV export from MySql to output
<?
$out = '';
$fields = dibi::fetchAll("SHOW COLUMNS FROM web_application");
foreach ($fields as $field)
{
$out .= '"'.$field->Field.'";';
}
$out = rtrim($out, ';');
$out .= PHP_EOL;
foreach ($this->model->csvExport() as $index => $row)
{
foreach ($row as $r)
{
$out .='"'.$r.'";';
}
$out = rtrim($out, ';');
$out .= PHP_EOL;
}
$out = iconv('utf-8', 'windows-1250', $out);
$filename = 'application-export___' . date('YmdHis') . '.csv';
header('Content-Encoding: windows-1250');
header('Content-type: text/csv; charset=windows-1250');
header('Content-Disposition: attachment; filename=' . $filename);
//echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
exit(0);
$this->view->setTemplateFile('modules/application/csv.php');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment