php 将数组从控制器传递到视图 - Codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13387729/
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
Pass array from controller to view - Codeigniter
提问by Raffaele Izzia
I tried to print the array in the controller, before passing it to a view and this is the output
我尝试在控制器中打印数组,然后将其传递给视图,这是输出
Array ( [annunci] => Array ( [0] => stdClass Object ( [EmailDatore] => [email protected] [Nome] => asdasd [Cognome] => asdas [IdAnnuncio] => 9 [Titolo] => sfsdfdsfshrea [Testo] => agrefdgdagdfg [Categoria] => [Sede] => [TipoContratto] => [Add_Date] => [Drop_Date] => )
[1] => stdClass Object ( [EmailDatore] => [email protected] [Nome] => asdasd [Cognome] => asdas [IdAnnuncio] => 10 [Titolo] => fafa [Testo] => fafaerea asdasdas dafasfd [Categoria] => [Sede] => [TipoContratto] => [Add_Date] => [Drop_Date] => ) ) )
I get the array from this method in my maincontroller
我在主控制器中从这个方法获取数组
public function get_annunci(){
$query=$this->user_model->annunci($this->email);
print_r($query);
}
I would like to pass this array to a view and then read the data. So i rewrite my method like this
我想将此数组传递给视图,然后读取数据。所以我像这样重写我的方法
public function get_annunci(){
$query=$this->user_model->annunci($this->email);
$this->load->view('main_view',$query);
}
In main view i have this
在主要视图中,我有这个
<div class="tab-pane active" id="annunci">
<ul>
<?php
print_r($annunci);
?>
</ul>
</div>
This is my error
这是我的错误
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: annunci
Filename: views/tab_annunci_view.php
Line Number: 4
采纳答案by Rick Calder
public function get_annunci(){
$query['annunci']=$this->user_model->annunci($this->email);
$this->load->view('main_view',$query);
}
You're passing the array but you aren't passing annunci as a variable.
您正在传递数组,但没有将 annunci 作为变量传递。
回答by toxicate20
This is because you have not defined $annunci and made it available to the view. You need to load it to the view first by
这是因为您尚未定义 $annunci 并将其提供给视图。您需要先将其加载到视图中
Controller
控制器
$data['id'] = $yourArray;
$this->load->view('your_view_file', $data);
View
看法
<?php print_r($id); ?> //prints $yourArray
回答by NACHIMUTHU RAMALINGAM
public function index()
{
$data=$this->model->viewBlog();
$this->load->view('blog/index',['data'=>$data]);
}
I hope this is work. if you handle n number of records use this method.
我希望这是工作。如果您处理 n 条记录,请使用此方法。
回答by Filkor
(Change $queryto $query ['annunci'])
OK, the $queryis an array already, then just change the view file to 'tab_annunci_view'
($query改为$query ['annunci']) OK,$query已经是一个数组了,那就把视图文件改成'tab_annunci_view'
So:
所以:
public function get_annunci(){
$query=$this->user_model->annunci($this->email);
$this->load->view('tab_annunci_view',$query);
}

