试图获取非对象的属性 - 未定义的变量:data / Codeigniter

时间:2016-01-17 18:17:33

标签: codeigniter

我试图从数据库获取news_id但是当去查看说:错误 - >试图获取非对象的属性/消息:未定义的变量:数据

此模型 - >

class Model1 extends CI_Model {
  public function get_art()
  {
    $query = $this->db->get('entries');
    return $query->result();
  }
}

这里是控制器代码 - >

class Home extends CI_Controller
{
  public function members()
  {
    $this->load->model('model1');
    $data=$this->model1->get_art();
    $this->load->view('members', $data);
  }
}

和此全视图 - >

<html>
<head>
    <meta charset="utf-8">
    <title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>
<? echo $data->body; ?>
</h1>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

This is because of you invalid pass data to view. At controller replace line

$data=$this->model1->get_art();

with

$data["query"] = $this->model1->get_art();

Then at view you will have var $query with results of your database query.

You can use it like this:

<h1>
<? foreach($query as $row) {
    echo $row->body;
} 
?>
</h1>