TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-28-2009, 01:38 PM   #1 (permalink)
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
Old 06-29-2009, 08:27 AM   #2 (permalink)
Jim
The Addict
 
Jim's Avatar
 
Join Date: Nov 2007
Location: the Netherlands
Posts: 281
Thanks: 2
Jim is on a distinguished road
Default

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 :)
__________________
Nunchaku! Who doesn't like martial arts? =)
Send a message via MSN to Jim Send a message via Skype™ to Jim
Jim is offline  
Reply With Quote
Old 06-29-2009, 08:56 AM   #3 (permalink)
The Acquainted
 
Hightower's Avatar
 
Join Date: May 2009
Location: Durham, UK
Posts: 134
Thanks: 9
Hightower is on a distinguished road
Default

Quote:
Originally Posted by Jim View Post
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.
__________________
Hightower's Softpolio
Send a message via MSN to Hightower
Hightower is offline  
Reply With Quote
Old 07-01-2009, 04:10 AM   #4 (permalink)
The Visitor
 
interactivedia's Avatar
 
Join Date: Jul 2009
Location: London
Posts: 3
Thanks: 0
interactivedia is on a distinguished road
Default

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.

---
Web Design - Web Development - Web Consulting
Send a message via MSN to interactivedia Send a message via Yahoo to interactivedia Send a message via Skype™ to interactivedia
interactivedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 11:01 PM
Next class project? allworknoplay The Lounge 6 04-18-2009 08:33 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM


All times are GMT. The time now is 04:27 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design