note 114175 added to function.fputcsv
| From: | iam at thatguy dot co dot za | Date: | Mon, 20 Jan 2014 14:37:28 +0000 |
| Subject: | note 114175 added to function.fputcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198288@lists.php.net to get a copy of this message | ||
So this is something I wrote to generically generate CSV exports from 2 arrays and a name.
You need to provide it with e single dimension array for the headings. A multidimensional array with
the actual body . And lastly the filename you would like to get minus the .csv and viola.
<?php
# We will use this function to generate a CSV
function exportCSV($headings=false, $rows=false, $filename=false)
{
# Ensure that we have data to be able to export the CSV
if ((!empty($headings)) AND (!empty($rows)))
{
# modify the name somewhat
$name = ($filename !== false) ? $filename . ".csv" : "export.csv";
# Set the headers we need for this to work
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=' . $name);
# Start the ouput
$output = fopen('php://output', 'w');
# Create the headers
fputcsv($output, $headings);
# Then loop through the rows
foreach($rows as $row)
{
# Add the rows to the body
fputcsv($output, $row);
}
# Exit to close the stream off
exit();
}
# Default to a failure
return false;
}
?>
----
Server IP: 113.20.13.217
Probable Submitter: 41.164.24.114
----
Manual Page -- http://php.net/manual/en/function.fputcsv.php
Edit -- https://master.php.net/note/edit/114175
Del: integrated -- https://master.php.net/note/delete/114175/integrated
Del: useless -- https://master.php.net/note/delete/114175/useless
Del: bad code -- https://master.php.net/note/delete/114175/bad+code
Del: spam -- https://master.php.net/note/delete/114175/spam
Del: non-english -- https://master.php.net/note/delete/114175/non-english
Del: in docs -- https://master.php.net/note/delete/114175/in+docs
Del: other reasons-- https://master.php.net/note/delete/114175
Reject -- https://master.php.net/note/reject/114175
Search -- https://master.php.net/manage/user-notes.php