dongzhang2150 2015-10-21 08:04
浏览 61
已采纳

将JSON数据放入View Codeigniter中

I am new in CodeIgniter. I want to put JSON data from controller to view in CodeIgniter. So far, I can show the JSON data directly from my controller.

Here is my code for controller:

public function index()
{
    //$this->load->view('test');
    $get_url_service = $this->url_service->GetUrl('KategoriRetrieve');
    $get_json = file_get_contents($get_url_service);
    $get_data = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($get_json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);
    //print_r ($get_data);
    foreach ($get_data as $key => $val) {
        if(is_array($val))
            echo "$key:"."<br />";
        else
            echo "$key => $val"."<br />";
    }
}

The function GetUrl() is loaded from my custom library called url_service. Here is my library url_service.php

<?php
    if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

    class Url_service
    {
        public function GetUrl($param){
        //http://localhost:90/Aset/json/reply/
        return "http://localhost:90/kugi_deployment/api/json/reply/".$param;
        }
    }

Like I already said above, I want to show the data in view. How can I do that? Can anyone help me to do this? Thanks

  • 写回答

1条回答 默认 最新

  • dongqinta4174 2015-10-21 08:32
    关注

    You can pass the data to the view from the controller.

    controller:

    ...
    $data['get_data'] = new RecursiveIteratorIterator(new RecursiveArrayIterator(json_decode($get_json, TRUE)), RecursiveIteratorIterator::SELF_FIRST);
    
    $this->load->view('myview', $data);
    ...
    

    view (in myview.php it in view directory):

    <?php
    
    foreach ($get_data as $key => $val) {
    ....
    }
    
    ?>
    

    http://www.codeigniter.com/user_guide/general/views.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件