2

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();
}
  • I believe there is an infinite loop occuring. why don't you use readfile function instead? php.net/manual/en/function.readfile.php – Nagarjun May 18 '15 at 9:01
  • Not the answer to your question, but JArrayHelper is deprecated use ArrayHelper instead ;-) – Sahil Purav May 18 '15 at 12:49
1

Your form might invoke a function that performs the export feature; given

<input name='task' value='normaltask'>

then in the script something like:

function exportCsvButton() {
    document.getElementById('task').value = 'export.csv';
    form.submit();
}

If you look at the other links on the page, they may be invoking a different function which is not resetting the task; hence once it's changed, it stays changed.

Simply add an instruction resetting the task, you could even put it in the exportCsvButton() function after you invoke form.submit().

Redirecting in the controller would not work, JApplication::close() is invoked appropriately to stop execution after the file has been transferred.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.