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.