0
votes

When I try to run a simple function it shows me:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Site::$model_users

Filename: controllers/site.php

Line Number: 14

Fatal error: Call to a member function get_news() on a non-object in /hermes/bosoraweb130/b418/ipg.blazewarcom/ci/application/controllers/site.php on line 14

The functions:

model_users.php located in models folder:

public function get_news()
{
    $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
    return $query;
}

site.php located in controllers folder:

public function home_news() {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
}
2

2 Answers

0
votes

Don't you just want to call the method get_news() on $this class?

Try

class Site {
   public function home_news() {
        $class_name = new Class_Name();  //change Class_Name() to the class that has the method get_news.
        $query = $class_name->get_news(); //This line caused the problem
        ...
        ...
   }
}

Hope this helps.

0
votes

This is how it should be:

Model:

class Model_users extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_news()
    {
        $query = $this->db->query("SELECT * FROM tblnews ORDER BY newId DESC LIMIT 3");
        return $query;
    }
}


Controller:

class Site extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('model_users');
    }

    public function home_news() 
    {
        $query = $this->model_users->get_news(); //This line causes the problem
        ...
        ...
    }
}