CodeIgniter Forums
Dynamic header & nav menu: how to handle them? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dynamic header & nav menu: how to handle them? (/showthread.php?tid=53479)



Dynamic header & nav menu: how to handle them? - El Forum - 07-25-2012

[eluser]Unknown[/eluser]
Hello everybody, I'm new to CI and I have to start a simple project.

I have a standard template for the 95% of my pages: header, horizontal nav menu, body and footer.

I read everything about templating, but there is always something that I cannot understand.

My menu is not a static menu: it's a megamenu that requires some query every time.

And in my header I have a dynamic top bar with some notification icons (like in Facebook) that requires query too...

The question is: have I to add all the queries, for the header and the menu, in every controller that call the pages with an header and a menu?

Or there is a smart way to do this?

Thank you


Dynamic header & nav menu: how to handle them? - El Forum - 07-25-2012

[eluser]Unknown[/eluser]
Hello,

One way you could do this is to make a MY_Controller file and in the contructor have it run the query and assign it to a variable like so:

Code:
class MY_Controller extends CI_Controller
{
    function __construct()
{
          
  parent::__construct();
                $this->load->model('headermodel');
  $this->headerstuff = $this->headermodel->get_header_features();
}
}

You would then extend all your controllers to use My_Controller like so:
Code:
class Yourcontroller extends MY_Controller {

}

You can then access the stuff in headerstuff like so if your model sends it back as needed:
Code:
$this->headerstuff ['someheaderpart']

This is how I would approach it. They may be another way that is easier but this is how I make my configuration options available in all controllers.

I hope this helps.

Regards,
Ray


Dynamic header & nav menu: how to handle them? - El Forum - 07-26-2012

[eluser]InsiteFX[/eluser]
Code:
class MY_Controller extends CI_Controller
{
    public function __construct()
    {
          
        parent::__construct();

        $data = array(
            'header'   => $your_header,
            'nav_menu' => $menu
        );

        $this->load->vars($data);
    }
}

Now you can access it just like any other view variable using $header etc;