• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do something on every page

#1
[eluser]FinalFrag[/eluser]
Hi there guys,

I'm working on this website and I have a problem.
I have a header.php file, which contains some code that has to be included on every page. This header.php also draws a menu on the screen.

At the moment, I have this in my controller
Code:
...
$data['menu'] = '<a href="#">home</a> | <a href="#">about</a>';
$this->load->view('home', $data);
...

In home.php I then include the header file which uses the $menu variable to draw the menu items.

The problem is that I have to define this $data['menu'] variable in ALL my controller (cause I need the menu on every page). Is there an easier way do this?

Cause in real life it's not as easy as $data['menu'] = ...
In real life, the data needs to be fetched from a database.

Is there any way for me to have the $data['menu'] variable in EVERY controller without me having to declare it all the time? (some kind of hook maybe?)

Thnx in advance...

#2
[eluser]Popcorn[/eluser]
I think this can be done via a sort of "master" controller as you like. You have to extend the controller class and then you can do your database fetching there.

Then your normal controllers would extend that.

That's an idea, not put into practice though.

Hooks are another option, but I can't remember the correct procedure for rendering the data from them. Hope someone can help me out there.

#3
[eluser]Frychiko[/eluser]
you could make it a member of your controller class..
Code:
class My_controller {
var $menu = array("bla, bla");

then in your view access the variable directly...

Code:
&lt;?=$this->menu?&gt;

so you can now cut it out from your controller function.

If it's across multiple controllers, you can put the menu in a library.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.