首页 > 解决方案 > 如何使用codeigniter制作文件.json?

问题描述

这是我的控制器中的 json 编码:

public function loaddata(){
    $sql = $this->db->query('select * from e_alat_angkutan')->result();

    return json_encode($sql);

}

如何制作文件 .json ?请帮忙

标签: phpjsoncodeignitercontrollerjsonencoder

解决方案


在控制器__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';
// }   

希望能帮助到你!


推荐阅读