在笨报告在笨报告(Reports in Codeigniter)

2019-06-12 06:28发布

什么是生成笨框架报告最simplist方法是什么? 是否有任何可用的库做这个任务? 除了制图什么其他的资源来做到这一点。

Answer 1:

找到了一个不错的解决方案自己。 如果你想生成CSV格式报告,它是很容易与笨。 你的模型函数

function index(){
return $query = $this->db->get('my_table');
    /*
    Here you should note i am returning 
    the query object instead of 
    $query->result() or $query->result_array()
    */
}    

现在,在控制器

function get_report(){
    $this->load->model('my_model');
    $this->load->dbutil();
    $this->load->helper('file');
    /* get the object   */
    $report = $this->my_model->index();
    /*  pass it to db utility function  */
    $new_report = $this->dbutil->csv_from_result($report);
    /*  Now use it to write file. write_file helper function will do it */
    write_file('csv_file.csv',$new_report);
    /*  Done    */
}

没有外部组件是必需的一切都在codeigntier可用。 干杯! 如果你想要写XML文件是一件容易的事。
只要使用xml_from_result() dbutil的方法和使用write_file('xml_file.xml,$new_report)访问这些链接,他们会有所帮助。

数据库工具类

文件助手



Answer 2:

完整的解释:

模型:

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

控制器:

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);
}
}


Answer 3:

我用这对我的.csv报告。 它在csv文件来更改数据库字段名称的能力。 这里是代码。

 public function export_csv() {
    $file_name = 'File_name_'.date("Y-m-d h-i-s").'.csv';
    $query = $this->db->query('SELECT 
        id as "Id", // id is table id and Id is the csv header field. 
        franchiseopt as "Nearest Location", 
        hear_about_us as "How did you hear about us?", 
        specify as "Specify",  
        email as "Email", 
        noguests as "Number of Guests", 
        eventdate as "Date of Event",
        name as "Your Name",
        phone as "Phone Number",
        locationevent as "Location of Event",
        message as "More Details"
        FROM TABLE_NAME ORDER BY id DESC');

    $this->load->dbutil();
    $data = $this->dbutil->csv_from_result($query);
    $this->load->helper('download');
    force_download($file_name, $data);  
    exit();
}

显然,你需要根据你的表来代替数据库表字段。



文章来源: Reports in Codeigniter