Tauras February 2016

Why using exit(); it stores only one row in csv file from selected data?

I am stuck on this one problem... For example, i am using exit(); it works, but it works only with first selected row, not with all. If i am using echo to display data, it displays perfecly. If i am trying to make it to work without exit(); its does not work at all. Any suggestions to how to fix this?

$vet = $_GET['vet'];
$name = $_GET['name'];
// etc...

$data = fopen('php://output', 'w');
fputcsv($data,array('ID', 'Name', 'Email address', etc...));

// selection query (it works perfecly)

foreach...

// echo query for tests (it works perfectly)

fputcsv($data, array($id, $name, $email, etc...));

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='. $name .'.csv');

exit();

endforeach....

Answers


Sougata February 2016

The headers & exit should be after the loop ends. Currently it is adding the first row and then exiting the loop. As a result only one row is present. The loop should run all the iterations to add all the rows to the file.

foreach...

// echo query for tests (it works perfectly)

fputcsv($data, array($id, $name, $email, etc...));

endforeach....

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename='. $name .'.csv');

exit();

Post Status

Asked in February 2016
Viewed 3,803 times
Voted 10
Answered 1 times

Search




Leave an answer