Need help exporting data to csv file using php and smarty

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anksrulz
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 12:39 pm

Need help exporting data to csv file using php and smarty

Post by anksrulz »

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.
anksrulz
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 12:39 pm

Re: Need help exporting data to csv file using php and smarty

Post by anksrulz »

the php code again :

$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);
?>
anksrulz
Forum Newbie
Posts: 3
Joined: Tue Dec 01, 2009 12:39 pm

Re: Need help exporting data to csv file using php and smarty

Post by anksrulz »

Well,

I tested a few more methods but I have not been able to get the new data yet.

The problem here is that if i do not log out and click the link it gives the same csv from the previous query. However, if i leave the page open and come back and try after say half an hour i get the new query results.

I need to fix this but i am not sure what is causing the delay.

Somebody help !!
Post Reply