Need help exporting data to csv file using php and smarty
Posted: Tue Dec 01, 2009 12:49 pm
Hi,
I am working on a website that uses php with smarty templates and oracle as db.
I have to export data from an query to a csv file with a click on the link posted at the website.
the code works for one time but when i generate the next csv without logging out of the website it brings out the previous result, i have tried ftruncate and ifexists but to no use.
any suggestions ??
i implemented the following code:
Template file:
{foreach name=list item=order from=$tickets}
{if $smarty.foreach.list.first}
<p><a href="abc.csv">Export to CSV</a></p>{/if}
.php file:
$list = array('CASEID,PRODUCTID,PLATFORM,TIMESTAMP,NAME,AFFILIATION');
$fname= "abc.csv";
$file = fopen($fname,"w");
//ftruncate($file,0);
foreach ($list as $line)
{
fputcsv($file,split(',',$line));
}
//$count = 0;
foreach($content["tickets"] as $value)
{
$list = "".$value[CASEID].",".$value[PRODUCTID].",".$value[PLATFORM].",".$value[TIMESTAMP].",".$value[NAME].",".$value[AFFILIATION];
fputcsv($file, split(',',$list));
//$count++;
}
fclose($file);
?>
However.
I am working on a website that uses php with smarty templates and oracle as db.
I have to export data from an query to a csv file with a click on the link posted at the website.
the code works for one time but when i generate the next csv without logging out of the website it brings out the previous result, i have tried ftruncate and ifexists but to no use.
any suggestions ??
i implemented the following code:
Template file:
{foreach name=list item=order from=$tickets}
{if $smarty.foreach.list.first}
<p><a href="abc.csv">Export to CSV</a></p>{/if}
.php file:
$list = array('CASEID,PRODUCTID,PLATFORM,TIMESTAMP,NAME,AFFILIATION');
$fname= "abc.csv";
$file = fopen($fname,"w");
//ftruncate($file,0);
foreach ($list as $line)
{
fputcsv($file,split(',',$line));
}
//$count = 0;
foreach($content["tickets"] as $value)
{
$list = "".$value[CASEID].",".$value[PRODUCTID].",".$value[PLATFORM].",".$value[TIMESTAMP].",".$value[NAME].",".$value[AFFILIATION];
fputcsv($file, split(',',$list));
//$count++;
}
fclose($file);
?>
However.