Welcome Guest, Not a member yet? Register   Sign In
Dynamic Footers

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.

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.


[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.

Thanks for all of the great answers. How would I go about calling the function from my view?

Edit: Ah, well it helps if I reread the post before jumping to conclusions. Smile

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

<?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

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

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:

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

class Includes {

   function footer_find_merchants()

      $CI =& get_instance();


      $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;



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

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

&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)
&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

$data = $this->includes->footer_find_merchants();

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.

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

&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

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:

// 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

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.

Theme © iAndrew 2016 - Forum software by © MyBB