在null上调用成员函数language()

时间:2016-08-12 00:35:52

标签: php codeigniter

我不知道为什么我在codeigniter 3.1.0中收到此错误我已经更换了系统但没有帮助。 Moldel DB.php:

<?php
class DB extends CI_Controller {
  public function show_videos($limit, $start) {
    $sql = 'SELECT * FROM videos ORDER BY id DESC LIMIT '.$start.', '.$limit;
    $data = $this->db->query($sql);
    return $data->result(); 
  }
} ?>

这是我的Controller Home.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {
    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *      http://example.com/index.php/welcome
     *  - or -
     *      http://example.com/index.php/welcome/index
     *  - or -
     * Since this controller is set as the default controller in
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://codeigniter.com/user_guide/general/urls.html
     */
  public function __construct() {
    parent::__construct();
    $this->load->library('pagination');
    $this->load->model('DB');
  }

  public function index() {
    $config['base_url'] = base_url().'/home/index';
    $config['total_rows'] = $this->db->count_all('videos');
    $config['per_page'] = 16;
    $config['uri_segment'] = 3;
    $choice = $config['total_rows'] / $config['per_page'];
    $config['num_links'] = floor($choice);
    $config['full_tag_open'] = '<div class="text-center"><ul class="pagination clearfix">';
    $config['full_tag_close'] = '</ul></div>';
    $config['first_link'] = false;
    $config['last_link'] = false;
    $config['first_tag_open'] = '<li>';
    $config['first_tag_close'] = '</li>';
    $config['prev_link'] = '<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>';
    $config['prev_tag_open'] = '<li class="prev">';
    $config['prev_tag_close'] = '</li>';
    $config['next_link'] = '<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>';
    $config['next_tag_open'] = '<li>';
    $config['next_tag_close'] = '</li>';
    $config['last_tag_open'] = '<li>';
    $config['last_tag_close'] = '</li>';
    $config['cur_tag_open'] = '<li class="active"><a href="#">';
    $config['cur_tag_close'] = '</a></li>';
    $config['num_tag_open'] = '<li>';
    $config['num_tag_close'] = '</li>';

    $this->pagination->initialize($config);
    $data['page'] = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
    $data['videos'] = $this->DB->show_videos($config['per_page'], $data['page']);
    $data['pagination'] = $this->pagination->create_links();

        $this->load->view('header');
        $this->load->view('index', $data);
    $this->load->view('footer');
    }
}

错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: DB::$load
Filename: libraries/Pagination.php
Line Number: 333
Backtrace:
File: C:\xampp\htdocs\application\controllers\Home.php
Line: 23
Function: model

File: C:\xampp\htdocs\index.php
Line: 315
Function: require_once

( ! ) Fatal error: Call to a member function language() on null in C:\xampp\htdocs\system\libraries\Pagination.php on line 333
Call Stack
#   Time    Memory  Function    Location
1   0.0004  153968  {main}( )   ...\index.php:0
2   0.0011  199112  require_once( 'C:\xampp\htdocs\system\core\CodeIgniter.php' )   ...\index.php:315
3   0.0113  886104  Home->__construct( )    ...\CodeIgniter.php:500
4   0.0193  1756456 CI_Loader->model( ) ...\Home.php:23
5   0.0198  1781248 CI_Controller->__construct( )   ...\Loader.php:353
6   0.0198  1782120 load_class( )   ...\Controller.php:75
7   0.0199  1784240 CI_Pagination->__construct( )   ...\Common.php:196
A PHP Error was encountered

Severity: Error

Message: Call to a member function language() on null

Filename: libraries/Pagination.php

Line Number: 333

Backtrace:

如果我评论第333行:$ this-&gt; CI-&gt; load-&gt; language('pagination');它有效,但我不确定这是解决方案。 我的默认语言是英语,我没有任何其他语言。

1 个答案:

答案 0 :(得分:1)

首先,模型中的第一行应为

class DB extends CI_Model