如何从查询结果转换为带标题和正文的CSV:codeigniter

时间:2017-11-16 08:16:01

标签: php codeigniter csv

我正在尝试为我的项目构建csv转换。

代码已完成,但只返回标题。

以下是控制器中的代码:

public function convcsv(){

        $list = $this->searchResult;
        $headerArray = array("Foreign Key", "Name", "Email", "Category");

        $header = str_replace(",", "", $headerArray);
        $datas = implode(',', $header) . "\r\n";

        foreach($list as $body)
        {
            $body = str_replace(",", "", $body);
            $datas.= implode(',', $body) . "\r\n";
        }

        $datas = mb_convert_encoding($datas, "SJIS-win", "UTF-8");

        $csvFileName = "phpList_" . date('Ymd_His') . ".csv";
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename=' . $csvFileName);
        header('Content-Transfer-Encoding: binary');
        while (ob_get_level() > 0)
        {
            ob_end_clean();
        }

        ob_start();
        print trim($datas);
        ob_flush();
        ob_end_clean();
        exit;
    }
控制器中的

$list = $this->searchResult是:

$this->searchResult = $this->user_model->get_users($searchKey, $config["per_page"], $page); 

我怎么可能得到身体?

0 个答案:

没有答案