Derian André Derian André - 3 months ago 27
PHP Question

Codeigniter: Call to a member function language() on nul

I don't know why I'm getting this error in codeigniter 3.1.0 i already replaced the system but didn't help.
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();
}
} ?>


This is my 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');
}
}


Error:

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:


If I comment line 333: $this->CI->load->language('pagination'); it works but im not sure that's the solution.
My default language is english, I do not have any other language.

Thanks!

Answer

For a start, the first line in your model should be

class DB extends CI_Model