php - 如何使用codeigniter制作文件.json?
问题描述
这是我的控制器中的 json 编码:
public function loaddata(){
$sql = $this->db->query('select * from e_alat_angkutan')->result();
return json_encode($sql);
}
如何制作文件 .json ?请帮忙
解决方案
在控制器__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);
}
正如这个答案所指出的,使用 CodeIgniter 写入 JSON 文件:
//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';
// }
希望能帮助到你!
推荐阅读
- python - 如何清理包含'[]'的nlp文本
- java - Java 方法引用的 TypeScript 等效项
- windows - 在 powershell ssh 会话中访问 Windows 网络驱动器
- python - 如何保存和恢复每个小部件实例唯一的小部件属性?
- callback - 散景 - 根据所选点更改平均值
- django-rest-framework - drf_yasg 文档参数未显示
- javascript - 在 insertAfter() 中使用 $(this).find('class')
- javascript - 一个说用户输入的机器人
- vb.net - 简化长行代码
- python - 外键多个图像未保存在 django 中