I am trying to export a list of items to a CSV file from the standard list view in a Joomla component. I have a simple export button and it creates a file for download.
The problem I am having is that the next link pressed it tries to export the data again until the form is refreshed. I'm guessing that's because I am not redirecting on the completion of the task.
Is there a way to cancel this task once its complete without redirecting? because redirecting will cause my file not to download.
This is my controller code which is executed after the list has been generated.
protected function exportData($data) {
// Set Headers
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename='.'shows.csv');
if($fp = fopen('php://output', 'w')) {
// Output the first row as column headings
if($data[0]) {
fputcsv($fp, array_keys(JArrayHelper::fromObject($data[0])));
}
// Output the rows
foreach ($data as $row) {
fputcsv($fp, JArrayHelper::fromObject($row));
}
// Close the file
fclose($fp);
}
JFactory::getApplication()->close();
}