Message: Undefined property: News::$new_model
本帖最后由 猪头三君 于 2012-8-22 22:35 编辑大家好,我第一次接触PHP及CodeIgniter框架,今天按照教程搭建一个简单的程序,但是在在加载页面时出现一个如下的错误:
A PHP Error was encounteredSeverity: Notice
Message: Undefined property: News : : $new_model
Filename: controllers/news.php
Line Number: 9
Fatal error: Call to a member function get_news() on a non-object in C:\xampp\htdocs\myphp\application\controllers\news.php on line 9
我的代码:
controllers/news.php
<?php
class News extends CI_Controller{
public function _construct(){
parent::_construct();
$this->load->model('News_Model');
}
public function index(){
$data['news'] = $this->new_model->get_news();//错误出现在这行
$data['title'] = 'News archive';
$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');
}
public function view($slug){
$data['news'] = $this->new_model->get_news($slug);
}
}
?>
models/news_model.php
<?php
class News_Model extends CI_Model{
public function _construct(){
$this->lode->database();
}
public function get_news($slug=FALSE){
if($slug===FALSE){
$query = $this->db->get('news');
return $query->result_array();
}
$qusry = $this->db->get_where('news',array('slug'=>$slug));
return $query->row_array();
}
}
?>
以上完全是教程里的代码,但是我这里会报错;求高手指点,万分感谢。
我也搜索了相关问题,得到一个解决方法是:
修改autoload.php 文件中的$autoload['libraries'] = array('database');
但是,对我无效。
$this->load->model('news_model');
class News_model extends ....
改成这样试试 $this->news_model->get_news才对,你把模型的名字写错了,少了一个s 谢谢楼上两位的回答,但是我修改错误后,问题依然存在 我也是这个问题,报同样的错,完全照着教程敲的,没敲错,谁知道这是为什么啊 Android将军 发表于 2014-3-5 22:27 static/image/common/back.gif
我也是这个问题,报同样的错,完全照着教程敲的,没敲错,谁知道这是为什么啊 ...
file:///C:\Users\gongchao\Documents\Tencent Files\1429602494\Image\Image2\GRAJDZ_I`OA{1107{%XOKJA.jpg
改成__construct(),前面有两条下划线,而不是一个下划线 #在这里快速回复#Parse error: syntax error, unexpected T_PUBLIC in C:\mycx\Apache2\Ci\application\controllers\news.php on line 20 Parse error: syntax error, unexpected T_PUBLIC in C:\mycx\Apache2\Ci\application\controllers\news.php on line 20
这个错误是什么原因, 这一行的代码是: public function index()
{
按手册写的呀,语法错在哪呀,求教 $this->lode->database(); 把这个写到getnews中 把getnews的构造函数删掉 试试
页:
[1]