php - 使用 CodeIgniter 写入 JSON 文件

标签 php mysql json codeigniter

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

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

}

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

<?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/>";
}

?>

这是我的 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 并再次回来了很多次,我已经完全搞砸了。任何帮助表示赞赏。

最佳答案

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

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

关于php - 使用 CodeIgniter 写入 JSON 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30744488/

相关文章:

PHP:->和::之间的区别

php - fatal error : Call to a member function query() on a non object

mysql - 一个月中的小时数 - mysql 查询

php - 在表单中发布数组值时未获取数组值

PHP正则表达式从字符串中提取带有模式的url

PHP 将数组保存到 SQL 问题

mysql - 无法使用 Sequelize 库连接三个具有 Sum 聚合函数的表

javascript - ES6 - 如何将 JSON 语法与模板文字保持一致?

javascript - 如何不显示重复值 Ng-repeat Angular JS

javascript - 正确抓取 JSON 数据