0

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
Madhawa Priyashantha
  • 9,427
  • 7
  • 31
  • 59
Jacqueline
  • 57
  • 8
  • 1
    any errors? what is $arr? – Madhawa Priyashantha Jul 20 '16 at 14:05
  • getData returns json_encoded data already, but you're encode it a second time. And $arr is undefined in the index method – Honk der Hase Jul 20 '16 at 14:13
  • @Jacqueline add errors to your post.don't comment errors.errors are very important – Madhawa Priyashantha Jul 20 '16 at 14:28
  • I have removed $arr, that shouldn't have been in there sorry. I seem to have problems with the fopen function and the fwrite function, although I have loaded the helper for this? I haven't written to a file before so maybe I am missing something else? – Jacqueline Jul 20 '16 at 14:29
  • @Jacqueline i repeat post errors – Madhawa Priyashantha Jul 20 '16 at 14:30
  • @Jacqueline don't post errors as commnets.add it to your post.just edit your question – Madhawa Priyashantha Jul 20 '16 at 14:37
  • @Jacqueline don't comment add it to your post – Madhawa Priyashantha Jul 20 '16 at 14:38
  • Apologies Fast Snail. I have now added the error messages to my original post. :) – Jacqueline Jul 20 '16 at 15:22
  • Too many errors. You need to narrow your focus here. Not just dump a bunch of code and hope others will do all of your debugging for you. – John Conde Jul 20 '16 at 15:39
  • I'm sorry you think I am doing that John Conde. I have been working hard to understand and learn the code. I was hoping that someone would explain where I am going wrong. – Jacqueline Jul 20 '16 at 15:50
  • Break this down into small steps. First learn how to write some text to the file with PHP. If that does not work then the path to your file is incorrect or you don't have the correct permissions to write to the file. When you have that working - then start working with getting simple data from the model. After that start working with Json data. – cartalot Jul 20 '16 at 18:42
  • Thanks cartalot that's what I have started doing. I have got rid of all the error messages but still l'm getting no file or message in my file. – Jacqueline Jul 20 '16 at 18:54

0 Answers0