Welcome Guest, Not a member yet? Register   Sign In
Where to put a "global variable" for the entire website
#1

[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?
#2

[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');
#3

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

[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?
#5

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

[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
#7

[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?
#8

[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)
#9

[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
}
#10

[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?




Theme © iAndrew 2016 - Forum software by © MyBB