Welcome Guest, Not a member yet? Register   Sign In
How to make vars available all around
#1

[eluser]smilie[/eluser]
Hi,

First of all - I know this question has been asked before - and I know that most common answer is to look in user guide (extending classess) and Phil Sturgeon's guide to base classess.

However, I still do not grasp the concept of it.

Let me put what I would like to achieve and if someone would be kind enough to point me to right direction.

Idea / problem:
I have a sidebar on the website which is visible on every page;
In that sidebar I want to place some statistics.

One way of doing it is to add to every function call to the model and pass it when loading view.
What I would like is to do that only once (somewhere?) and paste it to the view.

So, I believe there are 2 options:
- to create a generally available variable $stats which can be called from any function?
- to call a view in another controller (which is always loaded?).

Oke, I got so far (not working by the way):

Code:
MY_Controller:
class MY_Controller extends Controller
{
function __construct()
{
    parent::Controller();

    $this->load->model('stats_model');
    $this->data['stats'] = $this->stats_model->getStats();
  }
}

In all other controllers (function) which generate views:
Code:
class Something extends Controller
{
    function Something()
    {
        parent::Controller();
    }

    function whatever()
    {
        # Load pages
        $page['content'] = $this->load->view('content/stock_results',$data,TRUE);
        $page['header'] = $this->load->view('common/header','',TRUE);
        # This is where sidebar is beeing called, so I would like to pass results from above in here
        $page['sidebar'] = $this->load->view('common/sidebar',$this->data['stats'],TRUE);
        $page['footer'] = $this->load->view('common/footer','',TRUE);
        $this->load->view('home',$page)
    }
}

However, this does not work Sad And I would hate to have to copy & paste xx times same model load and stats function :'(

Please help! :-)

Cheers,
Smilie
#2

[eluser]ulugbek[/eluser]
Hi!

In this code just add:

Code:
class MY_Controller extends Controller
{

var $CI;
var $sidebar;
function MY_Controller()
{
    parent::Controller();

    $this->CI =& get_instance();

    $this->CI->load->model('stats_model');
    $data['stats'] = $this->CI->stats_model->getStats();
    $this->sidebar = $this->CI->load->view('common/sidebar', $data, TRUE);
  }
}

and in all controllers:

Code:
class Something extends MY_Controller
{
    function Something ()
    {
        parent::MY_Controller();
    }

    function whatever()
    {
        //load all other view blocks
        $data['sidebar'] = $this->sidebar;
        $this->load->view('home', $data);
    }
}

But the better way would be to create Template class, so you could load any sidebars, footer, header or whatever you want.

Hope it will help!
#3

[eluser]smilie[/eluser]
Hi ulugbek,

I am gonna give it a try tommorow - too tired...

$this->search->bed();
when(found)
{
sleep 8h;
}

:-)

Cheers,
Smilie




Theme © iAndrew 2016 - Forum software by © MyBB