sql >> Databasteknik >  >> RDS >> Mysql

Skriv till JSON-fil med CodeIgniter

Du loopar länderobjektet som en array. Ta en titt på det här

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



  1. Prestandaövervakning och revision PostgreSQL - Toppresurser

  2. Skriv GeoDataFrame till SQL Database

  3. MySQL SELECT Duplicerade rader från OpenCarts DataBase

  4. Hur ändrar man standardsorteringen för en tabell?