Last active
August 29, 2015 14:26
-
-
Save AnowarCST/13d2b87690e077f009aa to your computer and use it in GitHub Desktop.
Data export as CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function exportCSV($id, $data) { | |
header("Content-Type: application/vnd.ms-excel; charset=utf-8"); | |
header("Content-Disposition: attachment; filename=report_$id.csv"); | |
if ($data && count($data[0]) > 0) { | |
$rc = 0; | |
foreach ($data[0] as $key => $value) { | |
if ($rc > 0) { | |
echo ','; | |
} $rc++; | |
echo "$key"; | |
} | |
echo "\r\n"; | |
foreach ($data as $row): | |
$rc = 0; | |
foreach ($row as $key => $field_value): | |
if ($rc > 0) { | |
echo ','; | |
} $rc++; | |
if (empty($field_value)) { | |
} else if (strlen($field_value) > 10) { | |
echo '"' . addslashes($field_value) . '"'; | |
} else if (is_numeric($field_value)) { | |
echo $field_value; | |
} else { | |
echo '"' . addslashes($field_value) . '"'; | |
} | |
endforeach; | |
echo "\r\n"; | |
endforeach; | |
} else { | |
echo "Data Not Found!"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment