2015-06-09 2 views
2

Я очень новичок в php, mySql и CodeIgniter. Я пытаюсь прочитать из файла mySql и записать данные, прочитанные в файл JSON, используя CodeIgniter. Я могу читать данные из mySql и просматривать его и создавать JSON-файл, но полностью увязнул со всей информацией, которую я читаю в Интернете. Я искал всю ночь, чтобы увидеть, где мой код для записи в формате JSON, даже идет, то есть контроллер, вид, модель и т.д.Запись в файл JSON с использованием CodeIgniter

Это «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(); 
    } 

} 

Это мое мнение, которое работы (настройки, чтобы увидеть, если он читает 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"

<?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 и снова так много раз, я полностью испортил его. Любая помощь оценивается.

ответ

3

Вы перемещаете объект стран как массив. Посмотрите на это

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

Hi Carlos, Yeehaa! это оно :-). Большое вам спасибо за то, что нашли время, чтобы помочь мне. Очень признателен. Памела –

Смежные вопросы