tags:

views:

33

answers:

1

I have the PHP output, which is in table form, and I want that output in CSV format. How can I get that using PHP Smarty?

A: 

You should parse that PHP output with an xml parser. Use somethink like:

$phpOutput = ... ; //this is the output containing only the table
$xml = simplexml_load_string($phpOutput);
$csvOutput = "";
foreach ($xml->tr as $rows)
{
   $cells = array();
   foreach ($rows->td as $cell)
   {
      $cells[] = (string)$cell;
   }
   $csvOutput .= implode(",",$cells)."\r\n";
}

$smarty->assign("csv",$csvOutput);

Of course you must be careful and close every tag if you don't want to get warnings.

Zsolti
Thanks for your response,but i want to know that whether i can have a link to html o/p i.e export to csv so it can generate that o/p in csv.
chirs
This is not a problem, you have to access that page with a separate request and before outputing anything add some headers which will force the browser to show you the save file dialog. See more on http://php.net/header
Zsolti