3

我尝试在控制器中打印数组,然后将其传递给视图,这是输出

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] => ) ) )

我在我的主控制器中从此方法获取数组

 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
4

4 回答 4

5

这是因为您尚未定义 $annunci 并使其可用于视图。您需要先将其加载到视图中

控制器

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

看法

 <?php print_r($id); ?> //prints $yourArray
于 2012-11-14T21:41:14.857 回答
3
public function index()
    {
        $data=$this->model->viewBlog();
        $this->load->view('blog/index',['data'=>$data]);
    }

我希望这是工作。如果您处理 n 条记录,请使用此方法。

于 2018-06-20T06:36:29.020 回答
2

(更改$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);      
}
于 2012-11-14T21:38:29.890 回答
2
public function get_annunci(){
    $query['annunci']=$this->user_model->annunci($this->email);        
    $this->load->view('main_view',$query);      
}

您正在传递数组,但没有将 anninci 作为变量传递。

于 2012-11-14T22:25:25.920 回答