I am using fputcsv to create a csv file from a mysql table, everything works fine except if there's an empty column or NULL in the database table, as then the output csv cell is empty, rather than printing NULL.
Is this normal behaviour?
Heres the script im using
private function me_calendar_report() {
ini_set("memory_limit","256M");
$calendar = array();
$this->load->model('me_reports_model');
$calendar = $this->me_reports_model->get_all_resource_calendar();
$headers = array(
array('id', 'Resource ID', 'Job ID', 'From date', 'To date', 'Colour', 'Resource Type', 'Is provisional', 'Fixed price'
));
$calendar_data = array();
foreach($calendar as $i) {
$calendar_data[] = array(
$i->id,
$i->resource_id,
$i->job_id,
$i->from_date,
$i->to_date,
$i->colour,
$i->resource_type,
$i->is_provisional,
$i->fixed_price
);
}
$csv = array_merge($headers, $calendar_data);
$fp = fopen('report_repository/resource_calendar-' . date('d-m-Y') . '.csv', 'w');
foreach ($csv as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
return 'report_repository/resource_calendar-' . date('d-m-Y') . '.csv';
}
echo NULL;also just yields an empty output. But you could easily solve this by replacingNULLvalues with the string value'NULL'while populating the array in the loop.get_all_resource_calendar(), to changeNULLtoNULLvalues, ie.SELECT CASE WHEN `colour` IS NULL THEN 'NULL' ELSE `colour` END as `colour` OR in yourforeach($calendar as $i)` loop ->$i->colour = (trim($i->colour)=='') ? 'NULL':$i->colour;"value1",,"value3"and empty strings showed up like"value1","","value3"but I'm probably missing something. To work around nulls I am using JSON format instead of CSV for export/import.