View Single Post
Old 06-28-2009, 01:38 PM   #1 (permalink)
Hightower
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default CodeIgniter - globally used function

Hi all, I've recently started using CodeIgniter to code my next project, but it's the first time I've used a framework of any sort. My site follows a template using a generic header and footer, so loading a particular page might look like this:

PHP Code:
function index()
{
$this->load->view('header');
$this->load->view('index');
$this->load->view('footer');

The thing is, within the header file is the HTML code that shows the menu's and stuff, which is the same on each page. Some menu items need stuff working out first, so I did this:

PHP Code:
function index()
{
$next_fixture get_next_fixture()

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

private function 
get_next_fixture()
{
$fixtures $this->db->get('fixtures');

// Do rest of processing in this function....
// ...
// ...
return $whatever_array;

This works a treat, and in my header file I can just echo
PHP Code:
$date_of_fixture
etc wherever I need it in my menu bar.

Ok, now we get on to my problem

This function would need to be called and then passed to the header in each of the files I created. Because the function is to be shown on every page how would I go about making this function be called and displayed in the header view automatically?

I realise I would have to create a seperate file with the function in it, but would this file be a helper? A class??

And then, once I've created this so anything can access it, how would I go about calling it for every page so that the header view can just
PHP Code:
echo $some_variable_that_the_function_created
?

Thanks

I know it's a bit long winded but I've tried to give plenty of info so you guys can help.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote