CodeIgniter Forums
Creating a sidebar without duplicate code. - 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: Creating a sidebar without duplicate code. (/showthread.php?tid=30058)



Creating a sidebar without duplicate code. - El Forum - 05-01-2010

[eluser]Unknown[/eluser]
Hi All,

Was wondering if someone could help me.

Im trying to build a website with a sidebar on every page. Some of the the data in the sidebar is generated from my database but it is always the same query no matter what page you are on.

At the moment im assigning the query to my $data array in the index function of my first controller and then looping through the array in my sidebar to get my content, which works fine.

My question is, how do I get the same content to appear in the sidebar if I navigate to a different controller or function, without repeating my code in every function of every controller?

Any help would be much appreciated!

Cheers
Pete


Creating a sidebar without duplicate code. - El Forum - 05-01-2010

[eluser]imn.codeartist[/eluser]
[quote author="peterjharrison" date="1272745505"]Hi All,

Was wondering if someone could help me.

Im trying to build a website with a sidebar on every page. Some of the the data in the sidebar is generated from my database but it is always the same query no matter what page you are on.

At the moment im assigning the query to my $data array in the index function of my first controller and then looping through the array in my sidebar to get my content, which works fine.

My question is, how do I get the same content to appear in the sidebar if I navigate to a different controller or function, without repeating my code in every function of every controller?

Any help would be much appreciated!

Cheers
Pete[/quote]

Try this way it will solve you problem

1) Create a library and place in the system/application/libraries

File Name: MY_Controller.php

Code:
class Front_Controller extends Controller
{
     var $side_Bars;
     function Front_Controller()
     {
        parent::Controller();
        $this->load->model('my_model');
        $this->side_Bars=$this->my_model->get_my_navigations();
     }
}

now all of your front site controllers extends with above controller
Code:
class Test extends Front_Controller
{
   function Test()
   {
      parent::Front_Controller();
   }
  
   function index()
   {
   }
}

now no need to load navigation data.

you can go straight to your view and loop the

following code.
Code:
foreach($this->side_Bars as $row):
       // you codes
   endforeach;

You are done.