php Codeigniter 中的报告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11189021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reports in Codeigniter
提问by Muhammad Raheel
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 do this.
在 Codeigniter 框架中生成报告的最简单方法是什么?是否有任何库可用于执行此任务?除了绘制图表之外,还有哪些其他资源可以做到这一点。
回答by Muhammad Raheel
Found a nice solution myself. If you want to generate reports in csv format it is very easy with codeigniter. Your model function
自己找到了一个不错的解决方案。如果您想以 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()
*/
}
Now in controller
现在在控制器中
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 */
}
No externals are required everything is available in codeigntier. Cheers!
If you want to write xml file it is easy too.
Just use xml_from_result()method of dbutil and use write_file('xml_file.xml,$new_report)Visit these links they will help.
不需要外部工具,codeigntier 中的所有东西都可用。干杯! 如果你想写xml文件也很容易。
只需使用xml_from_result()dbutil 的方法并使用write_file('xml_file.xml,$new_report)访问这些链接,它们会有所帮助。
And
和
回答by Royal David
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);
}
}
回答by Adnan Ahmad
I used this for my .csv reports. it has the ability to change database fields name in csv file. here is the code.
我将它用于我的 .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();
}
Obviously You need to replace database table fields according to your table.
显然您需要根据您的表替换数据库表字段。

