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

Hello,

I'm new to CodeIgniter and PHP in general, but everyone starts somewhere. I am coding a login/register billing software and wanted to know how to pass variables to all views in the application. I have "configuration" variables where it gets the values from the database like company name, logo location, etc.

Thanks.
Reply
#2

you should put those in config file, then you can access these using config_item() in your views
Reply
#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
#4

Thanks for the solution.

I researched about it, and I saw that something like this is possible:
Code:
class MY_Controller extends CI_Controller {
    public function __construct() {
        parent::__construct();

        $globalConfig = array();

        $this->load->vars($globalConfig);
    }
}

And then extending MY_Controller with all my controllers. Will this work?
Reply
#5

(This post was last modified: 04-11-2015, 05:05 AM by casa.)

if you want to pass all the time the same variables in all the views and this variables not change, you can do like you say : extend CI_Controller with your MY_Controller and after all your controllers extend MY_Controller.
Note that :
My_Controler.php will be in directory named "core" of the directorys "application". No need to put it in the directory "core" of your directory "system".
application/core/MY_Controller.php
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
MY_Controller extends CI_Controller
{
    private 
$items ;
    
    public function 
__construct()
    {
        
parent::__construct() ;
        
$this->init() ;
        
$this->load->vars($this->items );
    }
    
    private function 
init()
    {
        
$this->items = array('some_var' => '....',
                     
'new_var' => '...') ;
    }    

Your other controllers in directory Controller
PHP Code:
defined('BASEPATH') OR exit('No direct script access allowed');

class 
extends MY_Controller
{
        private 
$vue '.......' ;

    public function 
__construct()
    {
        
parent::__construct() ;
    }
    
    
    public function 
index()
    {
        
$this->load->view($this->vue) ;
    }    

In your view
PHP Code:
$v $this->load->get_var('some_var') ; // or only call $some_var 
Reply
#6

For variables that will not fit in a config file you can pass them global using:

PHP Code:
$data = array();

$data['variable'] = 'new variable';

$this->load->vars($data); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB