Welcome Guest, Not a member yet? Register   Sign In
How to have some data always available at views
#1

[eluser]Jarno[/eluser]
Hello,


I'm new to CodeIgniter and I'm wondering how it's possible to always have some variables available at every view.
For example I set the site title in the database and want to display it in every view.


Yours,
Jarno
#2

[eluser]Sarfaraz Momin[/eluser]
use a config item

add the title to your config file .. something like below...
Code:
$config['site_title'] = "This is my site title";

use it in your views like this...

Code:
$this->config->item['site_title'];

-Sarfaraz
#3

[eluser]Jarno[/eluser]
[quote author="Sarfaraz Momin" date="1265935870"]use a config item

add the title to your config file .. something like below...
Code:
$config['site_title'] = "This is my site title";

use it in your views like this...

Code:
$this->config->item['site_title'];

-Sarfaraz[/quote]

What if the title is saved in a database?
#4

[eluser]cahva[/eluser]
Better to use $this->load->vars()
Code:
$array = array(
    'somevar' => 'something',
    'someothervar' => 'something else'
);

$this->load->vars($array)

// After that you can use $somevar and $someothervar in the views
#5

[eluser]Jarno[/eluser]
Where to define that piece of code?
I'm new to CI, I am used to my own little framework I created overtime, with Smarty.

I read a lot in the user guide but I'm still not familiar with the files etc. so hopefully you can be very clear.
#6

[eluser]cahva[/eluser]
$this->load belongs to loader class where you can find it in the user guide. You can use this in the constructor of your controller which would make those variables global in every function of that controller. Or define it in controller's function and it would be available only in that function.
#7

[eluser]SpooF[/eluser]
I would suggest creating a MY_Controller and then extend all your controls from it. Here's one made by Ben Edmunds

http://github.com/benedmunds/CodeIgniter...roller.php
#8

[eluser]jbreitweiser[/eluser]
You could also make a model that reads the database and initializes itself with the data. Then just use it to get the different values. Make sure to load the model on every page or include it via MY_Controller.
#9

[eluser]Jarno[/eluser]
Can someone explain this MY_Controller please? It seems some kind of "core" which is available through the whole project.
#10

[eluser]jbreitweiser[/eluser]
Its in the manual. Give it a read.

http://ellislab.com/codeigniter/user-gui...asses.html




Theme © iAndrew 2016 - Forum software by © MyBB