So we have the /View/Helper/CsvHelper.php
[php] class CsvHelper extends AppHelper { var $delimiter = ‘,’; var $enclosure = ‘"’; var $filename = ‘Export.csv’; var $line = array(); var $buffer; function CsvHelper() { $this—>clear();
}
function clear()
{
$this->line = array();
$this->buffer = fopen(‘php://temp/maxmemory:’. (5*1024*1024), ‘r+’);
}
function addField($value)
{
$this->line[] = $value;
}
function endRow()
{
$this->addRow($this->line);
$this->line = array();
}
function addRow($row)
{
fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure);
}
function renderHeaders()
{
header(‘Content-Encoding: UTF-8’);
header(‘Content-type: text/csv; charset=UTF-8’);
//header(‘Content-Type: text/csv’);
header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=".$this->filename);
echo "xEFxBBxBF";
}
function setFilename($filename)
{
$this->filename = $filename;
if (strtolower(substr($this->filename, -4)) != ‘.csv’)
{
$this->filename .= ‘.csv’;
}
}
function render($outputHeaders = true, $to_encoding = null, $from_encoding ="auto")
{
if ($outputHeaders)
{
if (is_string($outputHeaders))
{
$this->setFilename($outputHeaders);
}
$this->renderHeaders();
}
rewind($this->buffer);
$output = stream_get_contents($this->buffer);
if ($to_encoding)
{
$output = mb_convert_encoding($output, $to_encoding, $from_encoding);
}
return $this->output($output);
}
}
?>
We also have <strong>/View/Layouts/csv/default.ctp</strong>
[php]
<?php echo $content_for_layout;?>
[/php]
In /View/Scrappers/export_brute_csv.ctp we have:
[php]
$line= $items[0];
$this->Csv->addRow(array_keys($line));
unset($items[0]);
foreach ($items as $item)
{
$line = $item;
$this->Csv->addRow($line);
}
$filename=’Apartments_’ . date(‘Ymdhis’);
echo $this->Csv->render($filename);
[/php]
Now, ScrappersController.php contains this:
[php]
class ScrappersController extends AppController {
public $name = "Scrappers";
public $csv_header = NULL;
public $helpers = array(‘Html’, ‘Form’, ‘Csv’);
public function export_brute_csv(){
$this->set_csv_header();
$headers = array($this->csv_header);
Configure::write(‘debug’,0);
$this->loadModel(‘Accommodation’);
$raw_items = $this->Accommodation->find(‘all’);
$items = array();
foreach ($raw_items as $key=>$value){
$ik = $key + 1;
$items[$ik][‘id’] = $value[‘Accommodation’][‘id’];
$items[$ik][‘name’] = $value[‘Accommodation’][‘name’];
$items[$ik][‘description’] = $value[‘Accommodation’][‘description’];
$items[$ik][‘address’] = $value[‘Accommodation’][‘address’];
$items[$ik][‘postcode’] = $value[‘Accommodation’][‘postcode’];
$items[$ik][‘included’] = $value[‘Accommodation’][‘included’];
$items[$ik][‘facilities’] = $value[‘Accommodation’][‘facilities’];
$items[$ik][‘monthly_cost’] = $value[‘Accommodation’][‘monthly_cost’];
$items[$ik][‘deposit’] = $value[‘Accommodation’][‘deposit’];
$items[$ik][‘accommodation_type’] = $value[‘Accommodation’][‘accommodation_type’];
$items[$ik][‘bedrooms’] = $value[‘Accommodation’][‘bedrooms’];
$items[$ik][‘surface’] = $value[‘Accommodation’][‘surface’];
$items[$ik][‘rental_period’] = $value[‘Accommodation’][‘rental_period’];
$items[$ik][‘pets’] = $value[‘Accommodation’][‘pets’];
$items[$ik][‘finn_id’] = $value[‘Accommodation’][‘finn_id’];
$items[$ik][‘url’] = $value[‘Accommodation’][‘url’];
$items[$ik][‘date_published’] = $value[‘Accommodation’][‘date_published’];
$items[$ik][‘date_rented’] = $value[‘Accommodation’][‘date_rented’];
$items[$ik][‘date_expired’] = $value[‘Accommodation’][‘date_expired’];
$items[$ik][‘visit’] = $value[‘Accommodation’][‘visit’];
}
array_unshift($items,$headers[0]);
$this->layout = null;
$this->autoLayout = false;
$this->set(‘items’, $items);
}
public function set_csv_header(){
$headers = ‘id,name,description,address,postcode,included,facilities,monthly_cost,deposit,accommodation_type,bedrooms,surface,rental_period,pets,finn_id,url,date_published,date_rented,date_expired,visit’;
$headers = explode(‘,’, $headers);
foreach ($headers as $key=>$value){
$this->csv_header[$value] = $value;
}
}
}[/php]
That was used on apartments scrapper