从控制器向视图传递数组 - 笨(Pass array from controller to view

2019-07-03 19:51发布

我试图打印控制器中的阵列,将其传递给一个前视图,这是输出

Array ( [annunci] => Array ( [0] => stdClass Object ( [EmailDatore] => you@welcom.it [Nome] => asdasd [Cognome] => asdas [IdAnnuncio] => 9 [Titolo] => sfsdfdsfshrea [Testo] => agrefdgdagdfg [Categoria] => [Sede] => [TipoContratto] => [Add_Date] => [Drop_Date] => ) 
[1] => stdClass Object ( [EmailDatore] => you@welcom.it [Nome] => asdasd [Cognome] => asdas [IdAnnuncio] => 10 [Titolo] => fafa [Testo] => fafaerea asdasdas dafasfd [Categoria] => [Sede] => [TipoContratto] => [Add_Date] => [Drop_Date] => ) ) )

我得到这个方法在数组中我maincontroller

 public function get_annunci(){


    $query=$this->user_model->annunci($this->email);        
    print_r($query);        
    }

我想此数组传递给视图,然后读出的数据。 所以我重写我的方法是这样

public function get_annunci(){
    $query=$this->user_model->annunci($this->email);        
    $this->load->view('main_view',$query);      
}

在主视图中,我有这

<div class="tab-pane active" id="annunci">
    <ul>
        <?php
        print_r($annunci);
        ?>
    </ul>
</div>

这是我的错误

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: annunci

Filename: views/tab_annunci_view.php

Line Number: 4

Answer 1:

public function get_annunci(){
    $query['annunci']=$this->user_model->annunci($this->email);        
    $this->load->view('main_view',$query);      
}

你传递的数组,但你不及格annunci作为变量。



Answer 2:

这是因为你还没有定义$ annunci并使其提供给视图。 你需要给它首先加载到视图

调节器

$data['id'] = $yourArray;
$this->load->view('your_view_file', $data);

视图

 <?php print_r($id); ?> //prints $yourArray


Answer 3:

(改变$query$query ['annunci']确定, $query是一个数组已经,那么只需更改视图文件“tab_annunci_view”

所以:

public function get_annunci(){
    $query=$this->user_model->annunci($this->email);        
    $this->load->view('tab_annunci_view',$query);      
}


Answer 4:

public function index()
    {
        $data=$this->model->viewBlog();
        $this->load->view('blog/index',['data'=>$data]);
    }

我希望这是工作。 如果你处理的记录n个使用此方法。



文章来源: Pass array from controller to view - Codeigniter