I am trying to output my PHP code to a downloadable CSV file and so far i am able to do this however at the moment, the output does not place the field data into separate columns. Instead it puts everything into one column and in addition to that, it displays the postcodes in its neighbouring columns. Does anyone know why this is?
<?php
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header('Content-Disposition: attachment; filename="filename.csv"');
$outstream = fopen("php://output",'w');
if ($db_found) {
// Select all fields from bodyshops_master_network table
$SQL = "SELECT * FROM bodyshops_master_network";
$result = mysql_query($SQL);
while ( $db_field = mysql_fetch_assoc($result) ) {
// Splits the arrays
$row = explode(",", $db_field['postcodes_covered']);
$dealer_code = $db_field['dealer_code'];
$dealer_name = $db_field['dealer_name'];
$bodyshop_id = $db_field['bodyshop_id'];
$bodyshop_name = $db_field['bodyshop_name'];
$address1 = $db_field['address1'];
$address2 = $db_field['address2'];
$address3 = $db_field['address3'];
$address4= $db_field['address4'];
$address5 = $db_field['address5'];
$postcode = $db_field['postcode'];
$bo_tel = $db_field['BO_tel'];
$bo_email = $db_field['BO_email'];
$bc_name = $db_field['BC_name'];
$equity_contract = $db_field['equity_contract'];
echo "<pre>";
foreach ($row as $value) {
echo $dealer_code . " ";
echo $dealer_name . " ";
echo $bodyshop_id . " ";
echo $bodyshop_name . " ";
echo $address1 . " ";
echo $address2 . " ";
echo $address3 . " ";
echo $address4 . " ";
echo $address5 . " ";
echo $postcode . " ";
echo $bo_tel . " ";
echo $bo_email . " ";
echo $bc_name . " ";
echo $equity_contract . " ";
echo $value. "<BR>";
fputcsv($outstream, $row, ',', '"');
}
echo "</pre>";
}
mysql_close($db_handle);
} else {
print "Database NOT Found ";
mysql_close($db_handle);
fclose($outstream);
}
?>