从控制器传递数组以查看并显示它

时间:2014-03-10 04:39:05

标签: arrays codeigniter

我是codeigniter的新手。我想从控制器发送一个数组来查看并显示它。但是这个错误显示在视图中:

A PHP error was encountered   
Severity:Notice  
Message:Undefined variable:pub   
Filename:views/first.php  
Line number:25     

这是我的代码:
Cotroller

$data['pub']=$this->mymodel->get_public();
$this->load->view('first',$data);

模型

function pub_article()
   {
        $where=array('public'=>'1');
         $query=$this->db->get_where('article',$where);
        if($query->num_rows() > 0)
           return $query->result();
        else
          return false;
   }

视图

foreach($pub->result() as $row){
echo "<p>".$row->tiltl."</p>"
}

1 个答案:

答案 0 :(得分:1)

试试这段代码:

模特:

$where=array('public'=>'1');
$query=$this->db->get_where('article',$where);
if($query->num_rows() > 0)
   return $query;
else
   return false;

在您看来:

if($pub!=false)
{
  foreach($pub->result() as $row){
   echo "<p>".$row->tiltl."</p>"
  }
}