Skip to content

Instantly share code, notes, and snippets.

@AnowarCST
Created August 6, 2015 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AnowarCST/7abaf053a99ca215f581 to your computer and use it in GitHub Desktop.
Save AnowarCST/7abaf053a99ca215f581 to your computer and use it in GitHub Desktop.
export data in CSV with Zip. (Its so usefull to download the big size data)
<?php
function exportCSV_Zip($id, $data) {
$this->load->library('zip'); //load CodeIgniter Zip Library
$csv_data = '';
if ($data && count($data[0]) > 0) {
$rc = 0;
foreach ($data[0] as $key => $value) {
if ($rc > 0) {
$csv_data .= ',';
} $rc++;
$csv_data .= "$key";
}
$csv_data .= "\r\n";
foreach ($data as $row):
$rc = 0;
foreach ($row as $key => $field_value):
if ($rc > 0) {
$csv_data .= ',';
} $rc++;
if (empty($field_value)) {
//
} else if (strlen($field_value) > 10) {
$csv_data .= '"' . addslashes($field_value) . '"';
} else if (is_numeric($field_value)) {
$csv_data .= $field_value;
} else {
$csv_data .= '"' . addslashes($field_value) . '"';
}
endforeach;
$csv_data .= "\r\n";
endforeach;
} else {
$csv_data .= "Data Not Found!";
}
//data variablise
//make zip file with data
$folder_name = "reports_of_$id";
$name = "report_$id.csv";
$this->zip->add_data($name, $csv_data);
// $this->zip->archive(base_url()."csv/$folder_name.zip"); // Write the zip file to a folder on server. Name it "$folder_name.zip"
$this->zip->download("$folder_name.zip");// Download the file to desktop.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment