ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Undefined property error [Help Me]

December 04, 2011 11:04am

Subscribe [3]
  • #1 / Dec 04, 2011 11:04am

    Murvianto

    2 posts

    Hi there, please help me to solve this problem.

    This is my Controller code :

    class welcome extends CI_Controller {
    function __construct(){
    parent::__construct();
    }

      function index(){
    //use this for your homepage
    $data['title']= "Welcome to claudia's kids";
    $data['navlist']=$this->MCats->getAllCategoriesNav();
    $this->load->vars($data);
    $this->load->view('template');
    }

    This controller trying to load this model. This is my Model code :

    class MCats extends CI_Model {

    function __construct(){
    parent::__construct();
    }
     

      function getAllCategoriesNav(){
    $data=array();
    $Q=$this->db->get('categories');
    if($Q->num_rows()>0){
    foreach ($Q->result_array() as $row){
    $data[$row['id']]=$row['name'];
    }
          }
          $Q->free_result();
          return $data;
      }
    }

    Then i am entering the database configuration and change code in config/autoload.php

    This is the code before :

    /*
    | -------------------------------------------------------------------
    |  Auto-load Libraries
    | -------------------------------------------------------------------
    | These are the classes located in the system/libraries folder
    | or in your application/libraries folder.
    |
    | Prototype:
    |
    | $autoload['libraries'] = array('database', 'session', 'xmlrpc');
    */

    $autoload[‘libraries’] = array(’‘);

    then i change the code something like this :

    /*
    | -------------------------------------------------------------------
    |  Auto-load Libraries
    | -------------------------------------------------------------------
    | These are the classes located in the system/libraries folder
    | or in your application/libraries folder.
    |
    | Prototype:
    |
    | $autoload['libraries'] = array('database', 'session', 'xmlrpc');
    */

    $autoload[‘libraries’] = array(‘database’);

    And I already have set the database configuration like this :

    $active_group = ‘default’;
    $active_record = TRUE;

    $db[‘default’][‘hostname’] = ‘localhost’;
    $db[‘default’][‘username’] = ‘root’;
    $db[‘default’][‘password’] = ‘wicaksono’;
    $db[‘default’][‘database’] = ‘shoppingcart’;
    $db[‘default’][‘dbdriver’] = ‘mysql’;
    $db[‘default’][‘dbprefix’] = ‘’;
    $db[‘default’][‘pconnect’] = TRUE;
    $db[‘default’][‘db_debug’] = TRUE;
    $db[‘default’][‘cache_on’] = FALSE;
    $db[‘default’][‘cachedir’] = ‘’;
    $db[‘default’][‘char_set’] = ‘utf8’;
    $db[‘default’][‘dbcollat’] = ‘utf8_general_ci’;
    $db[‘default’][‘swap_pre’] = ‘’;
    $db[‘default’][‘autoinit’] = TRUE;
    $db[‘default’][‘stricton’] = FALSE;

    Then here is the errors show on browsers :

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: welcome::$MCats

    Filename: controllers/welcome.php

    Line Number: 11

    Fatal error: Call to a member function getAllCategoriesNav() on a non-object in C:\xampplite\htdocs\xampp\shopping\application\controllers\welcome.php on line 11

     

  • #2 / Dec 04, 2011 2:48pm

    InsiteFX

    6819 posts

    You need to change the following

    Controller to CI_Controller
    and
    Model to CI_Model

  • #3 / Dec 04, 2011 3:25pm

    scottwire

    41 posts

    You are autoloading the database, but you also need to load the model before making the function call:

    $this->load->model('MCats');
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases