Skip to content

Instantly share code, notes, and snippets.

@timw4mail
Created May 19, 2011 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timw4mail/981720 to your computer and use it in GitHub Desktop.
Save timw4mail/981720 to your computer and use it in GitHub Desktop.
CSV Generating Class
A simple class for exporting data from a database into a CSV file.
If you want to directly output the CSV, simply call
CSV::export($field_names, $data, $filename);
Where:
$field_names is an array, either associative, with the keys of the array being the column headers, or a traditional array, with the values of the array being the column headers of the csv in the order you want them.
$data is either an array or object of arrays or objects containing your data in the same order as in the $field_names array
$filename is the prefix of the filename you want
If you want to return the CSV file, to output elsewhere, put TRUE as a fourth parameter, and on the page you wish to output, send the following headers:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Accept-Ranges: bytes");
<?php
class CSV {
/**
* Export
*
* Exports a CSV file from php arrays
* @param array $field_names
* @param array/object $data
* @param string $filename
* @param bool $return
* @return file
*/
public static function export($field_names, $data=array(), $filename="export", $return=FALSE)
{
//Generate the title row
$title_row = self::_get_titles($field_names);
//Generate the body
$body = self::_format_data($data);
$filename = $filename."_".uniqid().".csv";
$output = $title_row . "\n" . $body;
//Standardize newlines
$output = strtr($output, "\r", "\n");
$output = str_replace("\n\n", "\n", $output);
if(!$return)
{
//Set the headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: private");
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
header("Accept-Ranges: bytes");
//Output the csv file
echo $output;
}
else
{
return $output;
}
}
/**
* Get Titles
*
* Generates title row of spreadsheet from array
* @param array $field_names
* @return string $title_row
*/
private function _get_titles($field_names)
{
$columns = array();
$col_keys = array_keys($field_names);
//Go through an arrays keys...add to columns if numeric
foreach($col_keys as $col)
{
if(is_string($col))
{
//Add valid columns to the array, make sure to escape " characters already there
$columns[] = '"'.strtr($col, '"', '\"').'"';
}
}
//What? No associative keys? I guess the columns are just the values of this array then
if(empty($columns))
{
foreach($field_names as $col)
{
//Add valid columns to the array, make sure to escape " characters already there
$columns[] = '"'.strtr($col, '"', '\"').'"';
}
}
$title_row = implode(",", $columns);
return $title_row;
}
/**
* Format Data
*
* Takes data array and converts it to CSV rows
* @param array/object $data
* @return string
*/
private function _format_data($data)
{
$body = '';
foreach($data as $row)
{
$row_array = array();
foreach($row as $col)
{
$row_array[] = '"'.strtr($col, '"', '\"').'"';
}
$body .= implode(",", $row_array)."\n";
}
return $body;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment