dongmu3187 2014-06-30 05: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?

  • 写回答

3条回答 默认 最新

  • duanbohan2015 2014-06-30 05: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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部