sql >> Base de Datos >  >> RDS >> Mysql

Escribir en un archivo JSON usando CodeIgniter

Está recorriendo el objeto de los países como una matriz. Echa un vistazo a esto

<?php
class European_countries extends CI_Controller {

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

public function index()
{
    $countries = $this->European_countries_model->get_countries(); 
    $data['countries'] = $countries;

    $response = array();
    $posts = array();
    foreach ($countries as $country) 
    { 
        $posts[] = array(
            "title"                 =>  $country->euro_id,
            "flag"                  =>  $country->flag_name,
            "population"            =>  $country->population,
            "avg_annual_gcountryth" =>  $country->avg_annual_gcountryth,
            "date"                  =>  $country->date
        );
    } 
    $response['posts'] = $posts;
    echo json_encode($response,TRUE);

    //If the json is correct, you can then write the file and load the view

    // $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';
    // }   
    // $this->load->view('european_countries_view', $data);
}}?>