TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   CodeIgniter - globally used function (http://www.talkphp.com/general/4638-codeigniter-globally-used-function.html)

Hightower 06-28-2009 01:38 PM

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.

Jim 06-29-2009 08:27 AM

You can create a helper in the application/helper/ folder e.g. /application/helper/myfunction_helper.php (include _helper or it wont work), then you can either load the helper in each file using $this->load->helper('myfunction'); as you already know. But you can also autoload it by adding it to the helper autoload array in application/config/autoload.php

CodeIgniter has a nice template parser, but it aint perfect imo :)

Hightower 06-29-2009 08:56 AM

Quote:

Originally Posted by Jim (Post 26276)
You can create a helper in the application/helper/ folder e.g. /application/helper/myfunction_helper.php (include _helper or it wont work), then you can either load the helper in each file using $this->load->helper('myfunction'); as you already know. But you can also autoload it by adding it to the helper autoload array in application/config/autoload.php

I've created a helper and autoloaded it for my project. To use the helper though I have to call the necessary function don't I? For instance:

PHP Code:

function index()
{
$data call_function_from_custom_helper();

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


The thing is, this helper (or function) needs to be loaded on every page, and the returned values need to be used on every page in the header file. I don't want to have to type the above code into every function of every class (or even the construct function of every class) as this is tedious. There must be somewhere where I can type the above code once, and once only so every page in my app automatically has the data from the helper function.

interactivedia 07-01-2009 04:10 AM

why not calling your function in the header, rather than calling it in the controller?

my advice doesn't follow CI's default model but there shouldn't be any problems.:-D

---
Web Design - Web Development - Web Consulting


All times are GMT. The time now is 08:20 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0