2012-06-25 62 views
14

在Codeigniter框架中生成報告最簡單的方法是什麼?有沒有任何圖書館可以做這項工作?除了繪製什麼是其他資源來做到這一點。Codeigniter中的報告

+0

以我的經驗,我沒有找到任何東西。我可以知道你正在尋找什麼類型的報告嗎? – LoganPHP

+0

我想從查詢生成報告。應該是任何格式的CSV,pdf或其他 –

回答

41

我自己找到了一個很好的解決方案。如果您想以csv格式生成報告,使用codeigniter非常容易。 您的模型功能

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) 訪問這些鏈接,他們將提供幫助。

Database Utility Class

而且

File Helper

+0

這是爲excel文件工作先生? – wewe

+0

@wewe我從來沒有測試過這個excel文件,但你可以測試並提及 –

+0

如何將一個void數據庫字段名稱?只有記錄顯示?在CSV – wewe

0

完整的解釋:

型號:

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

我用這個用於我的.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(); 
} 

顯然您需要根據您的表來替換數據庫表字段。