CakePHP placing array items into diferrent cells CsvView plugin

随声附和 提交于 2019-12-25 06:06:26

问题


I have started to export csv files with the help of CsvView plugin, but am stuck at one place.

So here is the controller code:

function export(){
        $data = array(
        array('37899810', '50001', '1', '7616'),
        );


        $_serialize = 'data';

         $this->response->download('export.csv');

        $this->viewClass = 'CsvView.Csv';
        $this->set(compact('data', '_serialize'));
}

The problem is that the whole array is printed in a single cell, where I would like every item of an array to be placed in its own cell.

Any help is much appreciated.


回答1:


this seems working for me :-

$result contains :-

Array
(
[0] => Array
    (
        [Eshop] => Array
            (
                [name] => test shop
            )

        [Productlps] => Array
            (
                [name] => product
            )

        [ProductViewLog] => Array
            (
                [credits_left] => 4999
                [ip_address] => 10.0.0.1
                [created] => 2013-12-02 06:25:07
            )

    )
)

$excludePaths = array(); // Exclude all id fields
$_extract = $this->CsvView->prepareExtractFromFindResults($results, $excludePaths);
$customHeaders = array('ProductViewLog.created' => 'Date Accessed','Eshop.name'=>'Shop Name','Productlps.name'=>'Product Name','ProductViewLog.credits_left'=>'Credit Balance','ProductViewLog.ip_address'=>"Accessed From");
$_header = $this->CsvView->prepareHeaderFromExtract($_extract, $customHeaders);

$_serialize = 'results';
$this->response->download('my_file.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('results', '_serialize', '_header', '_extract'));


来源:https://stackoverflow.com/questions/24570123/cakephp-placing-array-items-into-diferrent-cells-csvview-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!