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....

Related posts

1 comment

  1. 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();
    

Comments are closed.