Welcome Guest, Not a member yet? Register   Sign In
display same data from db automatically across multiple views - single function
#1

[eluser]martynrlee[/eluser]
Hi Guys,

Just wondering if anyone can help me...

I currently have a db with various page data stored such as contact details, page descriptions, introductions, headings etc.

I pull out the details from the model, depending on the function in the site controller. So for example, function about{} would pull in any details that match that page.

All works well but the contact details feature on all pages and they are also updated fairly often so they need to be dynamic. I know I could pull in the data from the db within every function in the controllers but just seems like there would be a way to just perform this function once and pull in to every page automatically. So I guess global variables but from a db.

I looked around and some people suggest using the config file but no mention of accessing a db from within the config file. Even if I could just pull the data in once at the top of the controller and send to all the views that seems logical but this doesn't seem to work.

Can anyone point me in the right direction? All help most appreciated.

Martyn.
#2

[eluser]techgnome[/eluser]
extend the base controller into a MY_Controller, put the code to pull the info into the constructor, then have your controllers extend that.

Here's an example of my MY_Controller that I use (CI2):
Code:
<?php
class MY_Controller extends Controller {
    
    var     $data = array();
  
    function __construct()
    {
        parent::Controller();    
        
        $this->data['content_view']     = '';
        $this->data['show_right_col']     = True;
        $this->data['css']                 = $this->config->item('css');
        $this->data['base']             = $this->config->item('base_url');
        $this->data['images']             = $this->config->item('images');
        $this->data['albumcovers']         = $this->config->item('albumcovers');
        
        $results = get_playing($this);
        
        $this->data['secondsremain']     = $results['secondsremain'];
        $this->data['nowplaying']         = $results['nowplaying'];
        $this->data['justplayed']         = $results['justplayed'];
        $this->data['upnext']             = $results['upnext'];
        $this->data['randompick']         = $results['randompick'];
        
    }
}

Then in my controllers you can see how I extend MY_Controller:
Code:
<?php

class Browse extends MY_Controller {

    function __construct()
    {
        parent::__construct();    
    }

And here's how I get the data, add to it and then pass it to the view:
Code:
$data = $this->data;
        $data['content_view'] = 'browse/browsemain';
        $data['show_right_col'] = false;
        
        $this->load->model("browse_model");
        $data['AlphaOptions']    = $this->browse_model->displayAlphaOptions();
        $data['YearOptions']    = $this->browse_model->displayYearOptions();
        
        $this->load->library('table');
        $this->load->helper('form');

        $this->load->view('template/template', $data);

-tg
#3

[eluser]martynrlee[/eluser]
Thanks techgnome,

Thats really useful, I like the idea of extending the controller.

Using this method though, would I not need to add something within every function (in each controller) like:

$data['show_contact_info'] = true; ?

What I am trying to acheive is a way to use this single script for the whole controller, rather than having to add a script to each function - is this possible using your suggested method?
#4

[eluser]techgnome[/eluser]
That's the kind of thing that goes into the constructor of the MY_Controller... more to the point, it would be:
$this->data['show_contact_info'] = true;

-tg




Theme © iAndrew 2016 - Forum software by © MyBB