Created
August 1, 2017 07:31
-
-
Save princealikhan/31d9d94b25f2a4f7aceaf471fb1d3021 to your computer and use it in GitHub Desktop.
Generate CSV from array using PHP (Codeigniter) in chunks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function arrayCsv(){ | |
$allUserCount = $this->db->count_all('users'); | |
$iteration = 2; | |
$userPerChunk = $allUserCount/$iteration; | |
$offset = 0; | |
$csvPath = '/Your-System-Path/'; | |
for ($i = 0; $i < $iteration ; $i++) { | |
$list = NULL; | |
$query = NULL; | |
$list = [ | |
array('Primary Id','Name', 'Email', 'Status'), | |
]; | |
$this->db->select('id,name,email,status,'); | |
$this->db->from('users'); | |
$this->db->limit($userPerChunk,$offset); | |
$query = $this->db->get(); | |
$result = $query->result_array(); | |
foreach ($result as $index => $item) { | |
$list[] = array($item['id'],$item['name'],$item['email'],$item['status']); | |
} | |
$offset = end($result)['id']; | |
$fileName = $csvPath.'your-csv-'.$i.'.csv'; | |
$fp = fopen($fileName, 'w'); | |
foreach ($list as $fields) { | |
fputcsv($fp, $fields); | |
} | |
fclose($fp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment