views:

537

answers:

2

Hi, I want to convert my sql data to csv files while clicking on a button. The code fragments I found for sql to CSV conversion were in PHP, and I'm trying to convert it to CakePHP since I'm working in CakePHP.

Here is the PHP code I'm tring to convert:

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
   while ($row = mysql_fetch_assoc($result)) {
        $csv_output .= $row['Field']."; ";
        $i++;
   }
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
    for ($j=0;$j<$i;$j++) {
           $csv_output .= $rowr[$j]."; ";
    }
    $csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;

SOLUTION

Function in the Controller:

function exporttocsv()
{       
 $this->set('headers',$this->Result->find('all',array('fields'=>'Result.label')));
 $this->set('values',$this->Result->find('all',array('fields'=>'Result.value')));    
}

exporttocsv.ctp file:

<?php

foreach($headers as $header):

  $csv_output .=$header['Result']['label'].", ";

endforeach;
$csv_output .="\n";

if(!empty($values)){
foreach($values as $value):

  $csv_output .=$value['Result']['value'].", ";

endforeach;
$csv_output .="\n";
}
else{
echo "There is no data to export.";
}

$filename = "export_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");

print $csv_output;

exit;
?>
+3  A: 

First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:

foreach ($results as $result) {
    echo join(', ', $result['COLUMNS']);
    echo "\n";
}

Outputs something like this:

value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...

Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).

deceze
Actually I do not want the results in the view.. I want it to be converted to a CSV file..Just to check if the values are obtained correctly,I did an echo in the controller.
Angeline Aarthi
You **do** want them in the View. A View is just **output**. The script in your question, it's just generating **output**. As NOSLOW points out, you just change how the browser handles it (page or file download?) via headers.
deceze
You are right.. I changed the code as you said. Had the queries in the controller and output the results in csv format in the view file along with the headers.. And now I get the desired output with the data in an excel file.. Thanks for your guidance.
Angeline Aarthi
+1  A: 

deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:

header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );

If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:

http://bakery.cakephp.org/articles/view/csv-helper-php5

I use it and it works well.

ya.. you are right.. Since I do csv download in only one place, I didn't go for the csv helper.. Thanks for your support.
Angeline Aarthi