title: Write to JSON file using CodeIgniter
body: <p>I'm very new to php, mySql and CodeIgniter. I'm trying to read from mySql file and write the data read to a JSON file using CodeIgniter. I can read the data from mySql and view it and can create JSON file but have gotten totally bogged down with all the different information I'm reading on internet. I've been searching all night to see where my code for writing to JSON even goes, ie Controller, View, Model etc.</p>
  
  <p>This is "European_countries_model.php"</p>
  
  <pre><code>// Model to access database
  class European_countries_model extends CI_Model {
  
      public function __construct()
      {
          $this-&gt;load-&gt;database();
  
      }
  
      public function get_countries()
      {
          // get data from table "european_countries"
          $query = $this-&gt;db-&gt;get('european_countries');
              return $query-&gt;result();
      }
  
  }
  </code></pre>
  
  <p>This is my view which works (set up to see if it was reading database ok):</p>
  
  <pre><code>&lt;?php
  
  // TEST - display data
  echo "Euopean Countries&lt;br/&gt;";
  
  foreach ($countries as $country){
      echo $country-&gt;euro_id."  ".$country-&gt;title."  ".$country-&gt;flag_name."  ".$country-&gt;population."  ".$country-&gt;avg_annual_growth."  ".$country-&gt;date."&lt;br/&gt;";
  }
  
  ?&gt;
  </code></pre>
  
  <p>This is my controller "European_countries.php"</p>
  
  <pre><code>&lt;?php
  
  // European countries controller
  class European_countries extends CI_Controller {
  
      public function __construct()
      {
          parent::__construct();
          $this-&gt;load-&gt;model('European_countries_model');
          // file helper contains functions that assist in working with files
          $this-&gt;load-&gt;helper('file');
          $this-&gt;load-&gt;database();
      }
  
      // pass all countries from model to view????
      public function index()
      {
          $data['countries'] = $this-&gt;European_countries_model-&gt;get_countries();
          //$this-&gt;load-&gt;view('european_countries_view', $data);
  
          $response = array();
          $posts = array();
          foreach ($countries as $country) 
          { 
              $title=$country['euro_id']; 
              $flag=$country['flag_name']; 
              $population=$country['population'];
              $avg_annual_gcountryth=$country['avg_annual_gcountryth'];
              $date=$country['date'];
  
              $posts[] = array('title'=&gt; $title, 
                  'flag_name'=&gt; $flag_name,
                  'population'=&gt;$population,
                  'avg_annual_gcountryth'=&gt;$avg_annual_gcountryth,
                  'date'=&gt;$date
                  );
          } 
          $response['posts'] = $posts;
  
          $fp = fopen('./eur_countries_array.json', 'w');
          fwrite($fp, json_encode($response));
  
  
  /*  
          if ( ! write_file('./eur_countries_array.json', $arr))
          {
              echo 'Unable to write the file';
          }
          else
          {
              echo 'file written';
          }   
  */
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>I think I've gone from following php to mySql to CodeIgniter and back again so many times, I've completely messed it up. Any help appreciated.</p>