【问题标题】:How to make file .json with codeigniter?如何使用codeigniter制作文件.json?
【发布时间】:2018-11-06 08:39:57
【问题描述】:
这是我在控制器中的 json 编码:
public function loaddata(){
$sql = $this->db->query('select * from e_alat_angkutan')->result();
return json_encode($sql);
}
如何制作文件 .json ?请帮忙
【问题讨论】:
标签:
php
json
codeigniter
controller
jsonencoder
【解决方案1】:
在控制器__construct()方法中加载文件助手:
public function __construct()
{
parent::__construct();
$this->load->helper('file');
}
然后用第二个参数将其回显为 TRUE:
public function loaddata(){
$sql = $this->db->query('select * from e_alat_angkutan')->result();
echo json_encode($sql,TRUE);
}
正如这个答案Write to JSON file using CodeIgniter:
//If the json is correct, you can then write the file and load the view
// $fp = fopen('./data.json', 'w');
// fwrite($fp, json_encode($sql));
// if ( ! write_file('./data.json', $arr))
// {
// echo 'Unable to write the file';
// }
// else
// {
// echo 'file written';
// }
希望对你有帮助!