嗨,我是Codeigniter的新手.遇到 PHP 错误


A PHP Error was encountered
Severity: Notice
Message: Undefined property: News::$News_model
Filename: controllers/News.php
Line Number: 14
Backtrace:
File: /Applications/MAMP/htdocs/workshop10/application/controllers/News.php
Line: 14
Function: _error_handler
File: /Applications/MAMP/htdocs/workshop10/index.php
Line: 315
Function: require_once

我的控制器:

defined('BASEPATH') OR exit('No direct script access allowed');
class News extends CI_Controller {
     public function index()
     {
          $data['theNews'] = array("All your base are belong to us!",
                                 "Autotune this news",
                                  "Numa Numa!!",
                                  $this->News_model->get_new_news()
                                );
          $this->load->view('templates/news_header');
          $this->load->view('new_items',$data);
          $this->load->view('templates/news_footer');
    }
}

我的模型:

class News_model extends CI_Model {
    var $title = '';
    var $content = '';
    var $date = '';
    public function __construct()
    {
    //call the Model constuctor
    parent::__construct();
    }
    public function get_new_news()
    {
    return "Something Kool";
    }
}

我的观点:

 <?php
 foreach ($theNews as $news_item) { ?>
 <div><?php echo $news_item; ?></div>
<?php } ?>

在你的控制器中。使用以下代码。

public function index()
 {
      $this->load->model('News_model');//loads your model 
      $return = $this->News_model->get_new_news();
      $data['theNews'] = array("All your base are belong to us!",
                             "Autotune this news",
                              "Numa Numa!!",
                              $return
                            );
      $this->load->view('templates/news_header');
      $this->load->view('new_items',$data);
      $this->load->view('templates/news_footer');
}

u 没有在控制器上加载模型文件

使用此$this->load->model('News_model');

最新更新