Welcome Guest, Not a member yet? Register   Sign In
How to pre-load MySQL data that is used in all pages
#1

[eluser]Ki[/eluser]
I have searched for this answer everywhere:
I am using a template approach with CI, where each controller loads a compiler template that loads views for static Header, Static Footer and displays Dynamic Content. Dynamic content is passed as variable from controller. Question: If I am using data from 3 MySql tables that is used everywhere - in header that appears on every page as well as in most dynamic content, how can I pre-load it? Caching aside, should I create a hook or a model, and then how can I make $data['query'] = $this->$db->get('TableName') run every time any page is open (or a header is open or better yet, run it as part of initialization procedure for the whole site)? I want to avoid pasting $data['query'] = $this->$db->get('TableName') in EVERY controller. Now I have it in a model, but still I have to call the function that contains $data['query'] = $this->$db->get('TableName') in a model on EVERY controller, which is also something I want to do once for the whole site.

Controler Example:
Code:
<?php
class Welcome extends Controller{
    
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
                
        //Load models that process data relevant to this page
        $this->load->model('Snipets');
        $this->load->model('Quick_links');

        // Load site-wide data that appears on every page

        $data['weather_data'] = $this->Weather->get_weather();
        $data['header_data'] = $this->Header->get_header();
        
        //we load the view partial as a string that will be passed to the container
        $content_data['view_content'] = '';        
        $data['content'] = $this->load->view('content/welcome_view', $content_data, TRUE);

        //set other data that will be used in the container
        $data['page_title'] = ' Welcome Page';
        
        //show the container
        $this->load->view('container_view', $data);
    }
    
}
?>

Container_View:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;?php $this->load->view('modules/meta');?&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php $this->load->view('modules/header_view');?&gt;
&lt;?=$content;?&gt;
&lt;?php $this->load->view('modules/footer_view');?&gt;
&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]Choo[/eluser]
You can extend base controller and write the data to it's property ($this->some_data). Or you can use a hook.
#3

[eluser]Ki[/eluser]
example of this particular hook anybody? would this be a post_controller hook?
#4

[eluser]jedd[/eluser]
Hi samota, and welcome to the CI forums.

You almost definitely want to look at [url="http://ellislab.com/forums/viewthread/109698/"]extending the core controller[/url] and putting these calls into there.

Jedd.




Theme © iAndrew 2016 - Forum software by © MyBB