CodeIgniter Forums

Full Version: Dynamic Footers
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]wdcci[/eluser]
Hello Everyone. I just wanted to say that so far I love Code Igniter's and this forum as well. Great posts!

I am new to Code Igniter and the idea of using a MVC system. I am working on a project that requires me to create a dynamic footer that consists of 4 columns (retail, professional, health, public services) of category specific businesses displaying the company name as a link to an internal company page. This information needs to be database driven as they will add new businesses. I would like to have a template or an include that calls this information when needed (on every page actually). I don't see how I can do this without having to call the same model and load the results into the variables inside every function in my controller class. Anyone have any ideas? Thanks.

El Forum

[eluser]valarkin[/eluser]
Hello wdcci, welcome to the CI forums and to CI itself!

What I would do is create a post controller hook that would append that data to what is in your display. This way the logic and code is in a single place and it allows you to have a cleaner controller. You can read up more on hooks in the user manual here: http://ellislab.com/codeigniter/user-gui...hooks.html.

Later

El Forum

[eluser]Pascal Kriete[/eluser]
There are a million ways of doing this. I would probably create a library to generate the output, autoload the library, and then call the proper function in the footer view.

A similar approach to the post controller hook would be to create an extended controller and give it an _output function (_output is automatically called after your controller executes). Then just add the needed code there.

El Forum

[eluser]wdcci[/eluser]
Thanks for all of the great answers. How would I go about calling the function from my view?

El Forum

[eluser]valarkin[/eluser]
Edit: Ah, well it helps if I reread the post before jumping to conclusions. Smile

El Forum

[eluser]Colin Williams[/eluser]
With PHP.

Code:
<?php print call_function() ?> or  <?php print $this->library->call_method() ?>

I'm not so sure appending content in the _output() method will work, because you don't want to end up with

Code:
&lt;/body&gt;&lt;/html><div id="footer">...</div>

El Forum

[eluser]wdcci[/eluser]
I am still having a problem with this. My PHP skills are fairly undeveloped, so bear with me.

I have a library called Includes that contains the following:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Includes {


   function footer_find_merchants()
   {

      $CI =& get_instance();

      $CI->load->model('includes_model');

      $data['retail']       = $CI->includes_model->find_categories(1);
      $data['consumer']     = $CI->includes_model->find_categories(2);
      $data['resturant']    = $CI->includes_model->find_categories(3);
      $data['professional'] = $CI->includes_model->find_categories(4);

      return $data;
   }


}

?&gt;

This class is automatically loaded in the autoload.php file.


Inside my view I try and call the the function with the following:

Code:
&lt;?php print $this->includes->footer_find_merchants(); ?&gt;



I get the following error when I run the page:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: retail

Filename: includes/footer.php

Line Number: 21

(Line 21)
Code:
&lt;?php foreach ($retail->result() as $row): ?&gt;
                  <li><a >url; ?&gt;" target="_blank">&lt;?php echo $row->merchant_name; ?&gt;</a></li>
               &lt;?php endforeach; ?&gt;


I understand something is off, i'm just not sure how to fix it.

When i add

Code:
$data = $this->includes->footer_find_merchants();
$this->load->view('home_index',$data);

to my controller, it works just fine. This defeats what I'm trying to do. I would like to just call the function from the footer that way I have just once maintain the code form the libray, the footer view and my model. Any help would be great. Thanks.

El Forum

[eluser]Colin Williams[/eluser]
There is a serious flaw in your logic.

Code:
&lt;?php print $this->includes->footer_find_merchants(); ?&gt;

This is going to literally print 'Array', because all you return from that method is an array.

Then you're trying to access $retail, when it is actually in $data['retail'] (actually, it's no where in the context of your View file). The reason it works from your controller is because $data is being extracted by the load->view() method into $retail, $consumer, $restaurant, $professional

El Forum

[eluser]eilrahc[/eluser]
wdcci: I asked a very similar question question a week or two ago. The solution in my case was using Colin's Template library.

I created a master template which contained the markup for a header, message area, content area, and footer. Then I simply write content to a region in the template with code like:

Code:
// Write some text into the 'page_title' region of the template
$this->template->write('page_title', 'Spiffy Title');
// Write another view into the 'content' region of the template, passing $data to it
$this->template->write_view('content', 'spiffy_view', $data);
// Send the completed markup to the browser
$this->template->render();

I don't know if the Template library is suitable for all projects, but so far I really dig it for mine. It drastically cut down the amount of repetition in my views at the cost of a few extra lines in my controller.