douhu2370 2014-08-04 05:01
浏览 45

搜索数据并导出到csv codeigniter

I just created function to export data to csv file and it works great. But now, I have to search the data first and show the result on the view and then export it as csv file.

For example, I search company by the location in Kuala Lumpur then it will show companies located in Kuala Lumpur. And then I have a button to export it as csv file, but I can't figure out how to get the data from view. Is there any logic to do this one? As far, here are my model:

function select_company_query() {
        $sql = 'select tb_company.id as company_id, tb_company.company_name, tb_company.sector, tb_company.phone, tb_company.email, '
                . 'tb_location.id as id_location, tb_location.prefer_location from tb_company, tb_location '
                . 'where tb_company.id = tb_location.company_id';

        $query = $this->db->query($sql);
        return $query;
    }

And my controller are:

function export($search_by, $search_field) {
        $this->load->dbutil();
        $this->load->helper('file');
        $path = '/var/www/company.csv';
        if ($search_by == 'FALSE') {
            $report = $this->company_model->select_company_query();

            $new_report = $this->dbutil->csv_from_result($report);

            if (!write_file($path, $new_report, 'c+')) {
                echo 'Unable to write the file!';
            } else {
                redirect(base_url('index.php/company'));
            }
        }
    }

Any help is appreciated. Thank you.

  • 写回答

1条回答 默认 最新

  • doudu5029 2014-09-05 16:06
    关注

    to expoert csv data you have to include a csv_helper file in controller you have just to do is

    $this->load->helper('csv'); query_to_csv($query, TRUE, 'searchfile.csv');

    $query is your result data

    评论

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害