Skip to content

Instantly share code, notes, and snippets.

@rainerlopez
Forked from opnchaudhary/csv_helper.php
Last active January 4, 2016 00:09
Show Gist options
  • Save rainerlopez/8539820 to your computer and use it in GitHub Desktop.
Save rainerlopez/8539820 to your computer and use it in GitHub Desktop.
Implemented possibility of using header on the array_to_csv method >>> Implemented possibility to change the default separator ( , )
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------
/**
* CSV Helpers
* Inspiration from PHP Cookbook by David Sklar and Adam Trachtenberg
*
* @author Jérôme Jaglale
* @link http://maestric.com/en/doc/php/codeigniter_csv
*/
// ------------------------------------------------------------------------
//-------------------------------------------
/*
Documentation:
==============
1. Array to csv file
array_to_csv($ArrayVariable, TRUE, 'file.csv')
2. Query to csv file
echo query_to_csv($query,'stalls.csv');
$query = $this->db->get('stall_contact');
$this->load->helper('csv');
3. Query to download file
$query = $this->db->get('stall_contact');
$this->load->helper('csv');
query_to_csv($query,TRUE,'bookings.csv');
*/
if ( ! function_exists('array_to_csv'))
{
function array_to_csv($array, $headers = TRUE, $download = "", $separator = FALSE)
{
if ($download != "")
{
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
}
ob_start();
$f = fopen('php://output', 'w') or show_error("Can't open php://output");
$n = 0;
if ($headers)
{
$line = array();
foreach ($array as $numbers)
{
foreach ($numbers as $key => $name) {
$line[] = $key;
}
break;
}
if ( ! fputcsv($f, $line))
{
show_error("Can't write line $n: $line");
}
}
foreach ($array as $line)
{
$n++;
if ( ! fputcsv($f, $line))
{
show_error("Can't write line $n: $line");
}
}
fclose($f) or show_error("Can't close php://output");
$str = ob_get_contents();
ob_end_clean();
if ($separator)
{
$str = str_replace(',', $separator, $str);
}
if ($download == "")
{
return $str;
}
else
{
echo $str;
}
}
}
// ------------------------------------------------------------------------
/**
* Query to CSV
*
* download == "" -> return CSV string
* download == "toto.csv" -> download file toto.csv
*/
if ( ! function_exists('query_to_csv'))
{
function query_to_csv($query, $headers = TRUE, $download = "", $separator = FALSE)
{
if ( ! is_object($query) OR ! method_exists($query, 'list_fields'))
{
show_error('invalid query');
}
$array = array();
if ($headers)
{
$line = array();
foreach ($query->list_fields() as $name)
{
$line[] = $name;
}
$array[] = $line;
}
foreach ($query->result_array() as $row)
{
$line = array();
foreach ($row as $item)
{
$line[] = $item;
}
$array[] = $line;
}
echo array_to_csv($array, FALSE, $download, $separator);
}
}
/* End of file csv_helper.php */
/* Location: ./system/helpers/csv_helper.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment