Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Last active August 29, 2015 14:19
Show Gist options
  • Save fredbradley/ebb018dfb6d08e0a6312 to your computer and use it in GitHub Desktop.
Save fredbradley/ebb018dfb6d08e0a6312 to your computer and use it in GitHub Desktop.
Useful scalable helper class for CSV puts and gets!
<?php
/**
* FBputcsv class.
*/
class FBcsv {
private $filename;
private $fp = false;
/**
* __construct function.
*
* @access public
* @param string $method (default: "w")
* @param mixed $csv_filename (default: null)
* @return void
*/
function __construct($method="w", $csv_filename=null) {
set_error_handler(array($this,"myhandleError"));
$this->csvfilename($csv_filename);
$this->fp = fopen($this->filename, $method);
}
/**
* csvfilename function.
*
* @access public
* @param mixed $custom_name
* @return void
*/
function csvfilename($custom_name) {
if ($custom_name)
$this->filename = $custom_name;
else
$this->filename = "csv_download_".$_SERVER['HTTP_HOST']."_".time().".csv";
}
/**
* myhandleError function.
*
* @access public
* @param mixed $errno
* @param mixed $errstr
* @param mixed $error_file
* @param mixed $error_line
* @return void
*/
function myhandleError($errno, $errstr,$error_file=null,$error_line=null) {
echo "<div class=\"alert alert-warning\"><p><strong>Error:</strong> [$errno] $errstr - $error_file:$error_line </p></div>";
die();
}
function write_error($msg) {
return "<div class=\"alert alert-warning\"><p><strong>Error #101:</strong> $msg </p></div>";
}
/**
* read function.
*
* @access public
* @param int $limit (default: 0)
* @param string $delimiter (default: ",")
* @return void
*/
function read($limit=0, $delimiter=",") {
$row = 1;
if ($this->fp !== FALSE) {
while (($data = fgetcsv($this->fp, $limit, $delimiter)) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$table_data[$row][$c] = $data[$c];
}
$row++;
}
if (isset($table_data)) {
return $table_data;
} else {
try {
throw new Exception('No table data could be read!');
} catch (Exception $e) {
echo $this->write_error($e->getMessage());
}
return false;
}
}
}
/**
* as_table function.
*
* @access public
* @param int $limit (default: 0)
* @param string $delimiter (default: ")
* @param mixed "
* @return void
*/
function as_table($limit=0, $delimiter=",") {
$data = $this->read();
if (empty($data)) {
try {
throw new Exception('No table data could be read!');
} catch (Exception $e) {
echo $this->write_error($e->getMessage());
}
return false;
}
echo "<table>";
echo "<tbody>";
foreach ($data as $row):
echo "<tr>";
foreach ($row as $column):
echo "<td>".$column."</td>";
endforeach;
echo "</tr>";
endforeach;
echo "</tbody>";
echo "</table>";
}
/**
* collate_csv function.
*
* @access public
* @param array $table (default: array())
* @param string $delimiter (default: ",")
* @return void
*/
function write(array $table=array(), $delimiter=",") {
if (empty($table)) {
try {
throw new Exception('There was no array from which to submit into a table!');
} catch (Exception $e) {
echo $this->write_error($e->getMessage());
}
return false;
}
// if has headers
$header_row = array_keys($table[0]);
fputcsv($this->fp, $header_row, $delimiter);
foreach ($table as $fields) {
fputcsv($this->fp, $fields, $delimiter);
}
return false;
}
public function outputCsv($assocDataArray) {
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $this->filename);
if(isset($assocDataArray['0'])){
$fp = fopen('php://output', 'w');
fputcsv($fp, array_keys($assocDataArray['0']));
foreach($assocDataArray AS $values){
fputcsv($fp, $values);
}
fclose($fp);
}
}
/**
* __destruct function.
*
* @access public
* @return void
*/
function __destruct() {
if ($this->fp !==false)
fclose($this->fp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment