I have managed to print out to screen my JSON file but I am unable to write to the file. Can anyone help, I'm a novice at PHP / codeIgniter but trying hard to learn. My code is as below.
Model Class
<?php
class DBModel extends CI_Model {
function getData() {
$query = $this->db->get('profile');
return json_encode($query->result());
} // end function
} // end class
?>
Controller Class
<?php
class Json extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('DBModel');
$this->load->helper('file');
}
public function index() {
$this->load->model('DBModel');
$data = $this->DBModel->getData();
// echo json_encode($data);
$fp = fopen('./artist_file.json', 'w');
fwrite($fp, json_encode($data));
if ( ! write_file('./artist_file.json', $arr))
{
echo 'Unable to write the file';
}
else
{
echo 'file written';
}
$this->load->view('jsonView', $data);
}
} // end of class
?>
These are the error messages I received
Filename: controllers/Json.php
Line Number: 19
Backtrace:
File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 19
Function: fopen
File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once
___________________________________________
A PHP Error was encountered
Severity: Warning
Message: fwrite() expects parameter 1 to be resource, boolean given
Filename: controllers/Json.php
Line Number: 20
Backtrace:
File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 20
Function: fwrite
File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once
__________________________________________
A PHP Error was encountered
Severity: Warning
Message: Missing argument 2 for write_file(), called in /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php on line 22 and defined
Filename: helpers/file_helper.php
Line Number: 85
Backtrace:
File: /Applications/XAMPP/xamppfiles/htdocs/Artists/application/controllers/Json.php
Line: 22
Function: write_file
File: /Applications/XAMPP/xamppfiles/htdocs/artists/index.php
Line: 315
Function: require_once