I'm working on a script to output csv files sorted on county(gemeente in Dutch), but this foreach loop only outputs the first county.
Here is my foreach loop
foreach($result as $gemeentearray){
$output = array();
$filename = $gemeentearray[0]['gemeente'];
$csv = new CSV(array('naam', 'objecttype', 'hoofdcategorie 1', 'subcategory 1', 'subsubcategory 1', 'hoofdcategorie 2', 'subcategory 2', 'subsubcategory 2', 'hoofdcategorie 3', 'subcategory 3', 'subsubcategory 3', 'icoon', 'lat', 'lng', 'straat', 'huisnummer', 'toevoeging', 'postcode', 'plaats', 'afbeelding', 'website', 'minisite', 'informatie', 'telefoonnummer', 'email', 'contact', 'id', 'afbeeldingen'));
foreach ($gemeentearray as $locatie) {
unset($locatie['gemeente']);
$csv->addRow($locatie);
}
$csv->export($filename);
$string = $csv;
unset($csv);
}
and here is the csv class
class CSV {
protected $data;
/*
* @params array $columns
* @returns void
*/
public function __construct($columns) {
$this->data = '' . implode(';', $columns) . '' . "
";
}
/*
* @params array $row
* @returns void
*/
public function addRow($row) {
$this->data .= '' . implode(';', $row) . '' . "
";
}
/*
* @returns void
*/
public function export($filename) {
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="' . $filename . '.csv"');
echo $this->data;
die();
}
public function __toString() {
return $this->data;
} }
This happens because you use die(); it terminates execution of the script and you see only first country. So you cant send multiple files in loop, you need something to tell the client where the content ends and a new header begins. You can either use the zip utility and package them up for download.