From 025217b2e1e862fa51b7ec32cd3ac6d50d5acd85 Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Thu, 29 Feb 2024 15:57:36 +1000 Subject: [PATCH] Only create CSV headers if we have data in output_helper. --- app/Helpers/output_helper.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/app/Helpers/output_helper.php b/app/Helpers/output_helper.php index 3591411bf..dedc6d5bd 100644 --- a/app/Helpers/output_helper.php +++ b/app/Helpers/output_helper.php @@ -178,18 +178,20 @@ function output_csv($instance) } // Our header line - $data_order_columns = array(); - $count = count($instance->resp->data); - for ($i=0; $i < $count; $i++) { - foreach ($instance->resp->data[$i]->attributes as $key => $value) { - if (!in_array($key, $data_order_columns)) { - $data_order_columns[] = $key; + if (!empty($instance->resp->data)) { + $data_order_columns = array(); + $count = count($instance->resp->data); + for ($i=0; $i < $count; $i++) { + foreach ($instance->resp->data[$i]->attributes as $key => $value) { + if (!in_array($key, $data_order_columns)) { + $data_order_columns[] = $key; + } } } + $instance->resp->meta->data_order = $data_order_columns; + $csv_header = $data_order_columns; + $output_csv = '"' . implode('","', $csv_header) . '"' . "\n"; } - $instance->resp->meta->data_order = $data_order_columns; - $csv_header = $data_order_columns; - $output_csv = '"' . implode('","', $csv_header) . '"' . "\n"; // Each individual data line $output_escape_csv = @$instance->config->output_escape_csv;