dongmu3187
2014-06-30 13:18
采纳率: 0%
浏览 114
已采纳

Codeigniter将数据从控制器传递到view / w复杂数组

Checked to see if this is a duplicate, it would appear so but the other questions I've found do not answer this question.

I'm using CodeIgniter and I would like to access a 'complex' array (from a db) in my view.

Codeigniter passing data from controller to view The link answers part of my question, but it's a simple array like so:

In the controller:

$data = array(
    'title' => 'title 1',
    'user' => 'user 1'
);
$this->layout->view('example/test', $data);

In the view

echo $title.$user;

So all of that I do get, however what if the array was a little more complicated like:

$data = array(
   array(
    'title' => 'title 1',
    'user' => 'user 1'
   ),
   array(
    'title' => 'title 2',
    'user' => 'user 2'
   )
);

How can I access this kind of array in my view?

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

检查这是否重复,它会出现,但我发现的其他问题没有回答 这个问题。

我正在使用CodeIgniter,我想在我的视图中访问一个'复杂'数组(来自数据库)。

Codeigniter将数据从控制器传递到视图 链接回答了我的部分内容 问题,但它是一个简单的数组,如下所示:

在控制器中:

  $ data = array  (
'标题'=>'标题1',
'用户'=>'用户1'
); 
 $ this-> layout-> view('example / test',$ 数据); 
   
 
 

在视图中

  echo $ title。$ user  ; 
   
 
 

所以我得到了所有这些,但是如果数组有点复杂如下:

 <  code> $ data = array(
 array(
'title'=&gt;'title 1',
'user'=&gt;'user 1'
  ),
 array(
'title'=&gt;  'title 2',
'user'=&gt;  'user 2'
)
); 
   
 
 

如何在视图中访问此类数组?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanbohan2015 2014-06-30 13:24
    已采纳

    you should wrap the outer array and give it a key like this

    $data = array(
       'myAwesomeArray' => array(
           array(
            'title' => 'title 1',
            'user' => 'user 1'
           ),
           array(
            'title' => 'title 2',
            'user' => 'user 2'
           )
       )
    );
    

    you should be able to access your data in the view using $myAwesomeArray

    已采纳该答案
    打赏 评论
  • douzhangshao6369 2014-06-30 13:29

    Use this in controller :

    $data = array(
                       array(
                        'title' => 'title 1',
                        'user' => 'user 1'
                       ),
                       array(
                        'title' => 'title 2',
                        'user' => 'user 2'
                       )
                    );
            $content = array('data'=> $data);
    $this->layout->view('example/test', $data);
    

    In view file :

    foreach($data as $key=>$val)
    {
      echo  $val['title'].$val['user'];
    }
    
    打赏 评论
  • duannaozhao4626 2014-06-30 13:44

    You can simply try this:

        $data['content'] = array(
                      array(
                            'title' => 'title 1',
                             'user' => 'user 1'
                            ),
                      array(
                             'title' => 'title 2',
                              ' user' => 'user 2'
                             )
                     );
    
    $this->load->view('example/test',$data);
    

    In View:

     foreach($content as $value)://to traverse the data array ....
    
     endforeach;
    
    打赏 评论

相关推荐 更多相似问题