CodeIgniter Forums
Where to put a "global variable" for the entire website - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Where to put a "global variable" for the entire website (/showthread.php?tid=13560)

Pages: 1 2


Where to put a "global variable" for the entire website - El Forum - 11-26-2008

[eluser]earlyriser[/eluser]
Hi.

I am making a site with the name "This is my temp name" and the only way I have found to pass this name in the a content view is with a $data['name'] = "This is my temp name". It happens I have multiple controllers where I have to do the same thing, then if the namesite changes, I will have to change it in different places.

I'm sure there is a way to declare the sitename in just one place and make the call to this variable from any controller, but I don't how. Maybe in the config file?

How do you do this?


Where to put a "global variable" for the entire website - El Forum - 11-26-2008

[eluser]kgill[/eluser]
You're correct, stick it in your config.php file:

$config['sitename'] = 'This is my temp name';

then you can use it anywhere in your app with the following:

$this->config->item('sitename');


Where to put a "global variable" for the entire website - El Forum - 11-26-2008

[eluser]earlyriser[/eluser]
thanks kgill
I knew it would be something really simple.


Where to put a "global variable" for the entire website - El Forum - 11-26-2008

[eluser]chakhar86[/eluser]
here is another idea:
If you only need the $name variable just show up to the view (e.g. the banner or title of website), try create a View file (call it header.php or so) then call it from the controller:
Code:
$this->load->view('header');
$this->load->view('contents', $data);
$this->load->view('footer');            // there you can add footer, and guess what? maybe some navigation panel?



Where to put a "global variable" for the entire website - El Forum - 11-26-2008

[eluser]earlyriser[/eluser]
Thanks chakhar86.
I was doing that.


Where to put a "global variable" for the entire website - El Forum - 12-29-2008

[eluser]simshaun[/eluser]
What if you need a global variable whose value comes from a model?
Wherever the variable is set, I need to be able to pass $CI->session->userdata('user_id') to the model's method.

Example:
$cur_user = $CI->UsersModel->get_user_data($CI->session->userdata('user_id'));

I've tried from the config, but its not letting me perform $CI->session->userdata('user_id'). (I'm assuming because the session lib hasn't been loaded at that point, but I don't know.)

Edit:
I found a temporary solution.

I've created this library which is autoloaded.
It loads the UsersModel and creates a key in the config just like I want.
Now I can call $this->config->item('cur_user_level') in my controllers.
Is this effective? If not, how should I do it?

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* Class is auto-loaded by CI.
*/
class AuthInit {

    var $CI;

    function AuthInit()
    {
        $this->init();
    }

    /**
    * Selectively set config items that contain the logged in user's data.
    */
    function init()
    {
        $this->_get_ci();
        $this->CI->load->model('UsersModel');

        $user_level    = $this->CI->UsersModel->get_user_level($this->CI->session->userdata('user_id'));

        $this->CI->config->set_item('cur_user_level', $user_level);
    }

    function _get_ci()
    {
        $this->CI =& get_instance();
    }
}

// end of file



Where to put a "global variable" for the entire website - El Forum - 12-30-2008

[eluser]xwero[/eluser]
simshaun why not add the user level to the session userdata once the user logs in instead of fetching on every page load and storing it in the config class?


Where to put a "global variable" for the entire website - El Forum - 12-30-2008

[eluser]chakhar86[/eluser]
@simshaun

use auto-load library

open system/application/config/autoload.php
register libraries/helpers/models that you want to load automatically (e.g. session lib)


Where to put a "global variable" for the entire website - El Forum - 12-30-2008

[eluser]xwero[/eluser]
chakhar86, simshaun is autoloading the library. But that is not the main problem.

The main problem is the library gets loaded when the user_id is in the session or not, making database calls when they aren't needed.

The cur_user_level data is only useful when a user is logged in or you have to see the not logged user as a user_level. but that can be checked as follows
Code:
if( ! $this->session->userdata('cur_user_level'))
{
   // not logged in
}
else
{
    // do something with cur_user_level here
}



Where to put a "global variable" for the entire website - El Forum - 12-30-2008

[eluser]chakhar86[/eluser]
ah, I think I misunderstood simshaun intention...

So you are saying that if 'user_data' session hasn't registered then you shouldn't make a database call instead just bring the user to login form?