Welcome Guest, Not a member yet? Register   Sign In
Making variables available in all views
#1

[eluser]Gram3000[/eluser]
Hi, I'm developing a CodeIgniter Application and I have some variables being retrieved from a database that I'd like to use on all views, whats the best way to go about this can anyone recommend?

Thanks
#2

[eluser]Clooner[/eluser]
[quote author="Gram3000" date="1255722596"]Hi, I'm developing a CodeIgniter Application and I have some variables being retrieved from a database that I'd like to use on all views, whats the best way to go about this can anyone recommend?

Thanks[/quote]

Code:
//In your controller:
$this->foo = "Available everywhere";

//In any view this should work
<?php echo $this->foo; ?>
#3

[eluser]Gram3000[/eluser]
Hi clooner,

Thanks for replying.

Can I create variables like those in Models too? Because I'd have multiple controllers with all the same variables being declared otherwise.
#4

[eluser]Clooner[/eluser]
[quote author="Gram3000" date="1255723050"]Hi clooner,
Can I create variables like those in Models too? Because I'd have multiple controllers with all the same variables being declared otherwise.[/quote]

No basically you have to use get_instance for that
Code:
// For within the model to access the controller data:
$CI =& get_instance();
$CI->foo;
#5

[eluser]jedd[/eluser]
Hi Gram3000 - what are you actually trying to achieve?

It sounds like you want a set of variables available in your views, your models, and your controllers - which might suggest you haven't yet fully appreciated the joy of the MVC model.

Check the [url="/wiki/FAQ/"]FAQ[/url] just in case your question has already been asked (and answered) a bunch of times. Try starting with the fourth question listed in the FAQ.

For variables you want to pull out of the DB and have pretty much everywhere, MY_Controller extension is the obvious answer. If you want stuff in your model(s) only, then extending MY_Model might make more sense. If you want variables in all your views - you might actually want, but not yet realise, that you want a common set of view partials to be generated instead.
#6

[eluser]Gram3000[/eluser]
Hi jedd,

Thanks for the details there, i'll read the FAQ.

Mainly what I want to do is to retrieve 5 or 6 variables from a DB table and have these available to different views. I can do this at the moment but it means repeating code in several controllers. I was wondering if there was a better way?
#7

[eluser]Clooner[/eluser]
[quote author="Gram3000" date="1255724184"]Hi jedd,

Thanks for the details there, i'll read the FAQ.

Mainly what I want to do is to retrieve 5 or 6 variables from a DB table and have these available to different views. I can do this at the moment but it means repeating code in several controllers. I was wondering if there was a better way?[/quote]

Use a my_controller. This is described in the manual: http://ellislab.com/codeigniter/user-gui...asses.html
#8

[eluser]Gram3000[/eluser]
Great, I'll look into this, Thank you.




Theme © iAndrew 2016 - Forum software by © MyBB