1

This is my json encode in my controller:

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

    return json_encode($sql);

}

how to make file .json ? please help

irufano
  • 108
  • 1
  • 11
  • Possible duplicate of [Write to JSON file using CodeIgniter](https://stackoverflow.com/questions/30744488/write-to-json-file-using-codeigniter) – pspatel May 27 '18 at 17:38
  • You mean headers or you want to write it to a file? – Alex May 28 '18 at 00:15

1 Answers1

0

In the controller __construct() method load the file helper:

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

And then echo it with second param to 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:

//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!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39