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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀