Welcome Guest, Not a member yet? Register   Sign In
Dynamic variable easy implementation
#1

[eluser]Designinglives[/eluser]
I'm currently looking for the best way to show a variable over all my pages without the need to make a $data['tokens'] variable in every single function we have in these controllers. The tokens I want to show will be variable and need to be taken from a database for each view. I was thinking about making a global variable but not sure if that's the right way to go. Declaring it in each controllers main function didn't work out for me.

Anyone has any suggestion?

Thanks in advance!
#2

[eluser]JoostV[/eluser]
Create a MY_Controller and have you controllers extend that. Fetch the tokens from the database in the MY_Controller __construct() method.

MY_Controller (in application/core folder)
Code:
class MY_Controller extends CI_Controller {

public $data;
    public function __construct(){
        parent::__construct();
        $this->load->model('tokens_m');
        $this->data['tokens'] = $this->tokens_m->get();
    }
}

Your Controller
Code:
class SomeController extends MY_Controller {

    public function index(){
        $this->load->view('some_view', $this->data);
    }
}

Use it in your view like this:
Code:
echo $tokens;

More on MY_Controller: http://codeigniter.tv/a-10/Extending-the...and-beyond
#3

[eluser]Designinglives[/eluser]
Thanks for the reply Joost but then I still have to edit each function in all the controllers? (we have about 1000 functions in these files) to get it displayed on every page? There should be some kind of function for it, no? Just to get a few variables which will always be needed for each and every page/function in your files?
#4

[eluser]JoostV[/eluser]
If you mean that you don't want to have to edit every $this->load->view() call, that's not a problem. You have access to $this in your views as well. So you can do:
Code:
echo $this->data['tokens'];

It's ugly, but it works Wink

If it's the fetching of the 'tokens' that's the problem: well, I'm not sure what parameters you need to fetch these 'tokens' form the database. But if you have access to them in MY_Controller, you can fetch it from the database there and you do not have to set it anywhere else. It will always be available in any controller that extends MY_Controller.

For instance, if you need the controller name and method to retrieve tokens form the database, you could do sth. like this in MY_Controller.

Code:
$controller = $this->router->fetch_class();
$method = $this->router->fetch_method();
$this->load->model('tokens_m');
$this->data['tokens'] = $this->tokens_m->get($controller, $method);

#5

[eluser]Designinglives[/eluser]
Thanks Joost, works perfect. You have solved one of my oldest problems as I've been looking for this for ages... I'm still a bit new to codeigniter it seems. Wink
#6

[eluser]JoostV[/eluser]
You're welcome. If you're interested, you can find more on MY_Controller and stuff like that @ the links in my signature Smile
#7

[eluser]Designinglives[/eluser]
Already on it, thanks! Tongue
#8

[eluser]InsiteFX[/eluser]
You can also use:
Code:
$data = array('title' => 'My Title');

$this->load->vars($data);    // variables are global to all views.

$this->load->view('Your_View_Name');





Theme © iAndrew 2016 - Forum software by © MyBB