简体   繁体   English

如何使用Codeigniter制作文件.json?

[英]How to make file .json with codeigniter?

This is my json encode in my controller: 这是我在控制器中的json编码:

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

    return json_encode($sql);

}

how to make file .json ? 如何制作.json文件? please help 请帮忙

In the controller __construct() method load the file helper: 在控制器的__construct()方法中加载文件帮助器:

public function __construct()
{
    parent::__construct();
    $this->load->helper('file');
}

And then echo it with second param to TRUE: 然后使用第二个参数将其回显为TRUE:

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

    echo json_encode($sql,TRUE);
}

As points this answer Write to JSON file using CodeIgniter : 作为重点,这个答案使用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';
// }   

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM