Reports in Codeigniter

后端 未结 3 820
醉酒成梦
醉酒成梦 2020-12-01 02:20

What is the most simplist way to generate reports in Codeigniter framework? Is there any library available to do this task? Except charting what are the other resources to d

3条回答
  •  自闭症患者
    2020-12-01 02:58

    Complete explanation:

    Model:

    class Csv_m extends MY_Model {
    function getCSV() {
        $sql = "SELECT * FROM tablename";
        return $this->db->query($sql);
    }
    }
    

    Controller:

    class Generate extends CI_Controller {
    var $file_path;
    public function __construct() {
        parent::__construct();
        $this->file_path = realpath(APPPATH . '../assets/csv');
    }
    function index() {
        $this->load->model('csv_m');
        $this->load->dbutil();
        $this->load->helper('file');
        //get the object
        $report = $this->csv_m->getCSV();
    
        $delimiter = ",";
        $newline = "\r\n";
        $new_report = $this->dbutil->csv_from_result($report, $delimiter, $newline);
        // write file
        write_file($this->file_path . '/csv_file.csv', $new_report);
        //force download from server
        $this->load->helper('download');
        $data = file_get_contents($this->file_path . '/csv_file.csv');
        $name = 'name-'.date('d-m-Y').'.csv';
        force_download($name, $data);
    }
    }
    

提交回复
热议问题