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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器