Hello,
If you want to add CSV export component using cake php, for that you can do by following steps:-
First take the controller file and create an action, the code will go like this:-
<?php
class Export_my_dataController extends AppController {
public function export() {
$this->response->download("export.csv");
$data = $this->Export_my_data->find('all');
$this->set(compact('data'));
$this->layout = 'ajax';
return;
}
}
The second this is to configure routes:-
In our routes.php file add the following:-
Router::parseExtensions('csv');
And finally set the action on view in view.php:-
Now you need to put View together, make sure escape value is correct as output-
<?php
foreach ($data as $val):
foreach ($val['export_my_data'] as &$cell):
// Escape double quotation marks
$cell = '"' . preg_replace('/"/','""',$cell) . '"';
endforeach;
echo implode(',', $val['export_my_data']) . "\n";
endforeach;
You have almost setup everything, Now you just need to link your CSV export from another View using the Html Helper, here you need to include the ext parameter for the file extension:-
echo $this->Html->url('export', array(
'controller' => 'export_my_data',
'action' => 'export',
'ext' => 'csv'
));
0 Comment(s)