douqufan9148 2015-11-19 07:24
浏览 45
已采纳

codeigniter新闻部分教程不起作用

hey guys im going thru the news section tutorial on codeigniter and i get the following error

A PHP Error was encountered

Severity: Notice

Message: Undefined property: News::$db

Filename: core/Model.php

Line Number: 77

Backtrace:

File: /Applications/MAMP/htdocs/CodeIgniter/application/models/News_model.php
Line: 13
Function: __get

File: /Applications/MAMP/htdocs/CodeIgniter/application/controllers/News.php
Line: 10
Function: get_news

File: /Applications/MAMP/htdocs/CodeIgniter/index.php
Line: 292
Function: require_once

All i did was simply copy what was provided in the tutorial, here are my files

news_model.php file

<?php

class News_model extends CI_Model {

    public function __constrcut() {
        $this->load->database();
    }

    public function get_news($slug = FALSE) {

        if ($slug === FALSE ) {

            $query = $this->db->get('news');
            return $query->result_array();
        }


        $query = $this->db->get_where('news', array('slug' => $slug));
        return $query->row_array();
    }
}

news.php

<?php 
class News extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('news_model');
    }

    public function index() {
        $data['news'] = $this->news_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->news_model->get_news($slug);

        if (empty($data['news_item'])) {
            show_404();

        }

        $data['title'] = $data['news_item']['title'];
        $this->load->view('templates/header',$data);
        $this->load->view('news/view',$data);
        $this->load->view('templates/footer');

    }

}

and here is a picture of my file structure

enter image description here

im trying my best to learn and debug but i dont know what it could be.

  • 写回答

1条回答 默认 最新

  • dongyuan1983 2015-11-19 07:30
    关注

    Maybe, database library is not loaded. you have to load database library.

    application / config / autoload.php

     $autoload['libraries'] = array('database');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能