Each day I run a cron where it finds data that has not been pulled/exported yet (via a column "exported" that contains a boolean) and this works fine. However, now I'd like to do a loop for each of the results and stick them in to the exported CSV in their own sheets grouped by the datetime.
My new CSV should look like:
Sheet 1 (12-04-2015) Sheet 2 (13-04-2015) Sheet 3 (14-04-2015)
And within those sheets display the grouped data that has the datetime column set for that day.
My current CSV export code:
$file = date('YmdHis');
$fp = fopen('/var/www/exports/'.$file.'.csv', 'w');
$count = 0;
while ($row = @mysql_fetch_array($query))
{
$data[] = array($row['email'],$row['first_name'], $row['last_name'], $row['datetime']);
if( strlen($cleared)>0 ) $cleared .= ",".$row['id'];
else $cleared = $row['id'];
$count++;
}
foreach ($data as $d) {
fputcsv($fp, $d);
}
fclose($fp);
Can I do this using fputcsv, and how would I do so, or would I have to use $objPHPExcel = new PHPExcel();
instead (which I know how to do)?
CSV does not support sheets, so you would need to go with something a bit more powerful, like PHPExcel
. New sheets can be created using $php_excel->createSheet();
method. You will get a PHPExcel_Worksheet
instance that you can populate.