Welcome Guest, Not a member yet? Register   Sign In
Setting a globally used function to run automatically
#7

[eluser]hightower[/eluser]
Ok Smile

I have a header view called header_view.php - this basically shows my HTML before content. A cut down (for simplisity) version is:

Code:
<html>
<head>
    <title><?= $page_title ?></title>
</head>
<body>
    <div class="leftmenuitem">
        <p>&lt;?= $date ?&gt;</p>
        <p>&lt;?= $h_team ?&gt;</p>
        <p>&lt;?= $a_team ?&gt;</p>
    </div>

    <div class="content">

    &lt;!-- Rest of content follows which is loaded by controller --&gt;

The above header_view.php uses 4 bits of information passed by the controller (it will in essence be many, many more bits of info when live). The page title is set in the controller, where as the last three bits of information are grabbed from the database using my custom function in the custom helper I created and autoloaded.

My controller looks something similar to this:

Code:
class Welcome extends Controller()
{

    function index()
    {
        $data = my_function_that_pulls_from_db();
        $data['page_title'] = 'Page Title 1';

        $this->load->view('header',$data);
        $this->load->view('content');
        $this->load->view('footer');
    }

    function another()
    {
        $data = my_function_that pulls_from_db();
        $data['page_title'] = 'Page Title 2';

        $this->load->view('header',$data);
        $this->load->view('another');
        $this->load->view('footer');
    }

}

I know I can put this code into a contructor statement, so I only need to insert it once per controller:

Code:
$data = my_function_that pulls_from_db();
$data['page_title'] = 'Page Title 2';

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

But my app is going to have quite a few controllers which means I will have to put that code in each one in order for my menu (in the header view file) to display its contents correctly - seems tedious.

So my question is, surely there is a location I can put:

Code:
$data = my_function_that pulls_from_db();
$data['page_title'] = 'Page Title 2';

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

So each page automatically loads the header (without me having to type extra code) so all I am left to do is:

Code:
class Welcome extends Controller()
{

    function index()
    {
        $this->load->view('content');
        $this->load->view('footer');
    }

    function another()
    {
        $this->load->view('another');
        $this->load->view('footer');
    }

}

I'm sorry if I'm not making sense - I just have a hard time putting into words the thoughts that go through my head Smile

All help is greatly appreciated.

Thanks!


Messages In This Thread
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 07:08 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 08:07 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 08:09 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 08:14 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 08:39 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 08:46 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 09:16 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 09:31 AM
Setting a globally used function to run automatically - by El Forum - 06-29-2009, 11:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB