Welcome Guest, Not a member yet? Register   Sign In
Passing variables to all views.
#3

(This post was last modified: 04-10-2015, 08:04 AM by CroNiX.)

He said they come from the database. Not all settings belong in a config file. Most clients don't have the ability to go editing php files.

I do something like that with application specific config settings. I just create a library that triggers a method in __construct() that gets the data from the db and creates variables from them. That library gets autoloaded so the variables are available globally.

It's fairly basic...something like:

PHP Code:
class Site_settings {

  
//holds the CI instance
  
protected $CI;

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

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

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

  public function 
init()
  {
    
//load the model that gets the data
    
$this->CI->load->model('site_config_model');

    
//retrieve/set the data
    
$this->site_config $this->CI->site_config_model->get_settings(); //returns an associative array of config settings
  
}


Now if you autoload the site_settings library, $this->site_config is the array that's available to all other controllers/models/views/etc.

PHP Code:
echo $this->site_settings['company_slogan']; 
Reply


Messages In This Thread
Passing variables to all views. - by iM7md - 04-10-2015, 04:40 AM
RE: Passing variables to all views. - by gadelat - 04-10-2015, 07:36 AM
RE: Passing variables to all views. - by CroNiX - 04-10-2015, 08:03 AM
RE: Passing variables to all views. - by iM7md - 04-10-2015, 01:04 PM
RE: Passing variables to all views. - by casa - 04-11-2015, 04:38 AM
RE: Passing variables to all views. - by InsiteFX - 04-11-2015, 04:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB