使用CodeIgniter写入JSON文件 [英] Write to JSON file using CodeIgniter

查看:109
本文介绍了使用CodeIgniter写入JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php,mySql和CodeIgniter的新手.我正在尝试从MySql文件中读取数据,并使用CodeIgniter将读取的数据写入JSON文件中.我可以从mySql中读取数据并进行查看,并且可以创建JSON文件,但是完全无法理解我在Internet上读取的所有不同信息.我整夜都在搜索以查看我写JSON的代码甚至在哪里,例如Controller,View,Model等.

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.

这是"European_countries_model.php"

This is "European_countries_model.php"

// Model to access database
class European_countries_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();

    }

    public function get_countries()
    {
        // get data from table "european_countries"
        $query = $this->db->get('european_countries');
            return $query->result();
    }

}

这是我的视图,可以正常工作(设置为查看数据库是否正常):

This is my view which works (set up to see if it was reading database ok):

<?php

// TEST - display data
echo "Euopean Countries<br/>";

foreach ($countries as $country){
    echo $country->euro_id."  ".$country->title."  ".$country->flag_name."  ".$country->population."  ".$country->avg_annual_growth."  ".$country->date."<br/>";
}

?>

这是我的控制器"European_countries.php"

This is my controller "European_countries.php"

<?php

// European countries controller
class European_countries extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('European_countries_model');
        // file helper contains functions that assist in working with files
        $this->load->helper('file');
        $this->load->database();
    }

    // pass all countries from model to view????
    public function index()
    {
        $data['countries'] = $this->European_countries_model->get_countries();
        //$this->load->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'=> $title, 
                'flag_name'=> $flag_name,
                'population'=>$population,
                'avg_annual_gcountryth'=>$avg_annual_gcountryth,
                'date'=>$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';
        }   
*/
    }
}

?>

我想我已经从跟随php到mySql到CodeIgniter,然后又回来了很多次,我完全搞砸了.任何帮助表示赞赏.

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.

推荐答案

您正在将国家对象作为数组循环.看看这个

You are looping the countries object as an array. Have a look at this

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

这篇关于使用CodeIgniter写入JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆