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

时间:2022-12-01 21:28:38

I am new with codeigniter.I want to send an array from controller to view and show it.but this error was show in view:

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

this is my code :
Cotroller

这是我的代码:Cotroller

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

Model

模型

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;
   }

View

视图

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

1 个解决方案

#1


1  

Try this code:

试试这段代码:

In model:

在模型中:

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

In your view:

在你看来:

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

#1


1  

Try this code:

试试这段代码:

In model:

在模型中:

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

In your view:

在你看来:

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