Welcome Guest, Not a member yet? Register   Sign In
Library not working
#1

I tried to implement a library which allows me to display global data like site name, site logo, etc from the database to all the views without the necessity of passing them via controllers(which I think is time consuming).


PHP Code:
<?php

class Settings {
 
   //holds the CI instance
 
   protected $ci;

 
   //the array the settings will be stored as
 
   public $settings = array();

 
   //
 
   public function __construct()
 
   {
 
       //load CI instance
 
       $this->ci =& get_instance();

 
       //fire the method loading the data
 
       $this->get_settings();
 
   }

 
   public function get_settings()
 
   {
 
       $this->ci->load->model('settings_model');
 
       $settings $this->ci->Settings_model->get_list();
 
       return $settings;
 
   }
}
?>


now in order to put it in my views, I've gotten to do just this:

PHP Code:
<?php echo $settings['website_name' ?>

This is what I get, two errors, one for the undefined property:


Quote:Severity: Notice
Message: Undefined property: Store::$settings_model
Filename: libraries/Settings.php
Line Number: 22
Backtrace:
File: C:\xampp\htdocs\codeigniter\app\libraries\Settings.php
Line: 22
Function: _error_handler

File: C:\xampp\htdocs\codeigniter\app\libraries\Settings.php
Line: 17
Function: get_settings

File: C:\xampp\htdocs\codeigniter\app\core\MY_Controller.php
Line: 8
Function: __construct

File: C:\xampp\htdocs\codeigniter\app\core\MY_Controller.php
Line: 34
Function: __construct

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

and one for the function calling in null(is there a way to overwrite this?):
Quote:An uncaught Exception was encountered
Type: Error
Message: Call to a member function get_list() on null
Filename: C:\xampp\htdocs\codeigniter\app\libraries\Settings.php
Line Number: 22
Backtrace:
File: C:\xampp\htdocs\codeigniter\app\libraries\Settings.php
Line: 17
Function: get_settings

File: C:\xampp\htdocs\codeigniter\app\core\MY_Controller.php
Line: 8
Function: __construct

File: C:\xampp\htdocs\codeigniter\app\core\MY_Controller.php
Line: 34
Function: __construct

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


I hope I could make myself clear.

Thanks in advance.
I do Front-End development most of the time 
Reply
#2

(This post was last modified: 03-21-2018, 04:20 AM by InsiteFX.)

Your missing self or $this on your $settings array

PHP Code:
// Wrong
public function get_settings()
{
 
   $this->ci->load->model('settings_model');
 
   $settings $this->ci->Settings_model->get_list();
 
   return $settings;
}

// Should be
public function get_settings()
{
 
   $this->ci->load->model('settings_model');
 
   $this->settings $this->ci->Settings_model->get_list();
 
   return $this->settings;


Also I think I ran into this with my Registry Class and had to make a helper with methods
to access the library methods.

So if that does not work then you will need to create a helper for the library.

Download registry here:

Registry for CodeIgniter
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(03-21-2018, 04:07 AM)InsiteFX Wrote: Your missing self or $this on your $settings array

PHP Code:
// Wrong
public function get_settings()
{
 
   $this->ci->load->model('settings_model');
 
   $settings $this->ci->Settings_model->get_list();
 
   return $settings;
}

// Should be
public function get_settings()
{
 
   $this->ci->load->model('settings_model');
 
   $this->settings $this->ci->Settings_model->get_list();
 
   return $this->settings;


Also I think I ran into this with my Registry Class and had to make a helper with methods
to access the library methods.

So if that does not work then you will need to create a helper for the library.

Download registry here:

Registry for CodeIgniter

Yes, you were right!. Thanks!
I do Front-End development most of the time 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB