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

将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

图片转代码服务由CSDN问答提供 功能建议

我是CodeIgniter的新手。 我想将控制器中的JSON数据放入CodeIgniter中查看。 到目前为止,我可以直接从我的控制器显示JSON数据。

这是我的控制器代码:

 公共函数索引(  )
 {
 // $ this-&gt; load-&gt; view('test'); 
 $ get_url_service = $ this-&gt; url_service-&gt; 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 =&gt; $ val){
 if(is_array($ val))
 echo“$ key:”。“&lt; br /&gt;”; 
 else 
 echo“$ key =&gt; $ val  “。”&lt; br /&gt;“; 
} 
} 
   
 
 

从我的 GetUrl()函数加载 自定义库名为url_service。 这是我的库url_service.php

 &lt;?php 
 if(!defined('BASEPATH'))exit('不允许直接访问脚本');  
 
类Url_service 
 {
公共函数GetUrl($ param){
 // http:// localhost:90 / Aset / json / reply / 
 return“http:// localhost:90 / kugi_deployment  /api/json/reply/".$param;
} 
} 
   
 
 

就像我上面已经说过的,我想在视图中显示数据。 我怎样才能做到这一点? 任何人都可以帮我这样做吗? 谢谢

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题