【问题标题】:Write to JSON file using CodeIgniter使用 CodeIgniter 写入 JSON 文件
【发布时间】:2015-08-25 00:08:17
【问题描述】:

我对 php、mySql 和 CodeIgniter 非常陌生。我正在尝试从 mySql 文件中读取数据并将读取的数据写入使用 CodeIgniter 的 JSON 文件。我可以从 mySql 读取数据并查看它,并且可以创建 JSON 文件,但我完全陷入了我在互联网上阅读的所有不同信息中。我整晚都在搜索,看看我的 JSON 写入代码在哪里,即控制器、视图、模型等。

这是“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();
    }

}

这是我的有效视图(设置以查看它是否可以读取数据库):

<?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”

<?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 又回来了很多次,我完全搞砸了。任何帮助表示赞赏。

【问题讨论】:

    标签: php mysql json codeigniter


    【解决方案1】:

    您将国家对象作为数组循环。看看这个

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

    【讨论】:

    • 嗨,卡洛斯,耶哈!而已 :-)。非常感谢您花时间帮助我。非常感激。帕梅拉
    猜你喜欢
    • 2016-12-13
    • 2021-12-03
    • 2016-05-31
    • 2017-12-16
    • 1970-01-01
    • 2017-06-29
    • 2020-02-14
    • 1970-01-01
    • 2017-11-28
    相关资源
    最近更新 更多