I’m still rather amateurish at PHP… how do I export the following as a downloadable csv file. I tried various functions off the net that exported all the results from the query right to csv but this is not what im after, what I want to do is export the following specific fields from the query, the foreach results in 1 row of the csv file and I also want to include column headings. The following code is an example structure but what do I add to make it export as a CSV?
if($_GET['task'] == "export_failed"){
$query = "SELECT * FROM food_order, client, takeaway WHERE client.client_id = food_order.order_customer_id AND takeaway.takeaway_id = food_order.takeaway_id AND processed=0";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$orders[] = $row;
}
$i=0;
foreach($orders as $order){
if($i == 0){ //make column headings
echo "Delivery Date/Time";
echo "Customer First Name";
echo "Customer sureName";
echo "Takeaway id";
echo "takeaway name";
}
else { //rows in csv file
$order['delivery_time'];
$order['client_firstname'];
$order['client_surname'];
$order['takeaway_id'];
$order['takeaway_name'];
}
$i++;
}
}
To output web content to a csv file check out:
Creating a CSV file with PHP
His/her sample code shows the setup of the page itself as well as the outputting of the headers and specific columns.
btw, regarding the headers, note that the column headers are outputted before the loop of data rows. How you have your code, the first row in your return data set won’t get outputted because the loop is busy doing the header instead.