CodeIgniter, how to make some data from your database available throughout to all of your views

Huh, I know why you are here, welcome and all my compassion.

I too spent 2 hours to get this out of the most popular light weight framework online documentation and huge fanbase. CI is more sweeter than a piece of Cake (;)), according to hardcore fans, but certainly their documentation is pain in the...well leave it.

To the point:

I want some data to be always available to all of my views regardless of which page I'm into in. For example it could be a drop-down of states in the right hand corner of my header menu. How do I do it?

Solution:

Create a helper file, say my_lil_helper.php and keep it under application/helpers. By the way you can do all the good things loosely, usually you do inside your controller, inside a helper. So it could be loading a model, a library, whatever.
So you write a function there like this:

function return_states()
{
//Model call to fetch data from db table and return
return $state_array;
}

In you header file, all you have to do is, use the get_instance method like this:

$CI =& get_instance();
$CI->load->helper('my_lil');

//Now call that lil function to generate states
$states = return_states();

//Loop array and do whatever you want

...and that's it

Hope you appreciated my solution too with my compassion, thanks :D

Cheers!

Comments

Popular posts from this blog

uTorrent: Data error (cyclic redundancy check)

Khude Jajabor Istasi

Photo upload to Facebook from your PHP web application