php codeigniter:将数组从控制器传递到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8756207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
codeigniter: pass array from controller to view
提问by Irakli
I have CodeIgniter question. How can I pass an array from controller to view? Here is my code that doesn't work:
我有 CodeIgniter 问题。如何将数组从控制器传递到视图?这是我的代码不起作用:
controller:
控制器:
$data_part13['header3_item'][] = array('title' => 'first image 1' , 'img' => 'https://encrypted-tbn0.google.com/images?q=tbn:ANd9GcQoshslL3aMNzG50708domqPSA4ouPjk_wA7jCpVRUH3k8zVdn9' );
$this->load->view('part_1_3', $data_part13);
and view:
并查看:
<div id="header3">
<div id="header3-inner">
<?php
if (isset($header3_item)){
foreach ($header3_item as $key) {
?>
<div class="header3-item">
<img alt="<?php echo($key->title); ?>" src="<?php echo($key->img); ?>"/>
</div>
<?php
}
}
?>
</div>
</div>
回答by
You did it correctly (kinda). You passed an array to the view, but your problem was that you were using an object in the view. You should have instead done something like this:
你做对了(有点)。您将一个数组传递给视图,但您的问题是您在视图中使用了一个对象。你应该做这样的事情:
$data_part13['header3_item'][] = (object) array('title' => 'first image 1' , 'img' => 'https://encrypted-tbn0.google.com/images?q=tbn:ANd9GcQoshslL3aMNzG50708domqPSA4ouPjk_wA7jCpVRUH3k8zVdn9' );
$this->load->view('part_1_3', $data_part13);
The view part can stay the same.
视图部分可以保持不变。
回答by Dan Blows
You're passing it in correctly, but you're not accessing it correctly from the view. Instead of $key->title
, you need to use $key['title']
;
您正确地传递了它,但是您没有从视图中正确地访问它。取而代之的是$key->title
,您需要使用$key['title']
;