Possible Duplicate:
Create a CSV File for a user in PHP
I am looking for a solution to create a CSV download link from a mysql query for my little project.
Basically the download link will be in this format :
http://localhost/vnodes/generateExcel.php?fromDate=2012-07-23&toDate=2012-07-24&location=1
in which my generateExcel.php will contain this :
<?php
$fromDate = $_GET['fromDate'];
$toDate = $_GET['toDate'];
$location = $_GET['location'];
include "connect.php";
$connect = mysql_connect($dbhost, $dbusername, $dbpassword) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$query = mysql_query("SELECT * FROM `nodes` WHERE `dateIn` BETWEEN '$fromDate' AND '$toDate' AND partLocation = '$location' ORDER BY `dateIn` ASC") or die(mysql_error());
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=report.csv");
header("Pragma: no-cache");
header("Expires: 0");
while($result = mysql_fetch_array($query)) {
echo $result['dateIn'].",".$result['partNumber'].",".$result['serialNumber'].",".$result['4serialNumber'].",".$result['status'].",".$result['operator']."\n";
}
?>
The thing I do not know is how to make a CSV out of the $query
output and trigger a download.
Thank you for any help!