Mysql
 sql >> Base de données >  >> RDS >> Mysql

Écrire dans le fichier JSON à l'aide de CodeIgniter

Vous bouclez l'objet pays sous forme de tableau. Regarde ça

<?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);
}}?>