CodeIgniter Forums

Full Version: How to call member functions within a class?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is my original code:

Code:
   

class Truelovecafe extends CI_Controller
{
   function _Truelovecafe()
   {
       $this->load->helper('url');
   }

   public function view($page = "index")
   {
       
       if ( ! file_exists(APPPATH.'/views/truelove_view/'.$page.'.php'))
           ...
       else if ($page=="contact-us")
       {
           $this->load->view('truelove_view/templates/header.php');
           $this->load->view('truelove_view/'.$page);    
           $this->load->view('truelove_view/templates/contact_footer.php');
       }
       else
       {
           $this->load->view('truelove_view/templates/header.php');
           $this->load->view('truelove_view/'.$page);    
           $this->load->view('truelove_view/templates/footer.php');    
       }
   
   }}

Then I took out first two lines and put them in a separate function with the purpose of lessen lines of code and to try calling a member function.

Code:
   private function load_head_page()
   {
       $this->load->view('truelove_view/templates/header.php');
       $this->load->view('truelove_view/'.$page);    
   }

And then:

Code:
       else
       {
           $this->load_head_page();
           $this->load->view('truelove_view/templates/footer.php');    
       }

But it's giving me a fatal error saying header.php is undefined...
Can you paste the full error here? If your header.php is in the right folder structure to be found, maybe it's the $page variable in your load_head_page that is undefined and the error trace is just near the included header.php file.
(03-17-2015, 11:48 AM)logsdon Wrote: [ -> ]Can you paste the full error here? If your header.php is in the right folder structure to be found, maybe it's the $page variable in your load_head_page that is undefined and the error trace is just near the included header.php file.

Oh, ok so I guess Im calling load_head_page() correctly? That's what I was worried about. Thank you.
The code works only if I include $this->load->helper('url'); in all functions one by one, but it doesnt work when I include it in the constructor like this:

Code:
function _construct()
{
parent::_construct();
$this->load->helper('url');
}
Solved, was using a single underscore instead of a double one.. duh..
May I suggest another, imo, a bit simpler and cleaner solution for you.

Instead of breaking out the view loading of the header and page, just set the footer template name in a variable.

Start by setting a default template, the regular footer used on all pages except contact us page. If the page is contact us, we just change the footer template variable to what footer we want to load.

Here is an example
PHP Code:
public function view($page "index")
{
 
 // Default footer template
 
 $footer_template 'footer';

 
 if ( ! file_exists(APPPATH.'/views/truelove_view/'.$page.'.php'))
 
 {
 
   // Something
 
 }
 
 else if ($page == "contact-us")
 
 {
 
   // Change footer template
 
   $footer_template 'contact_footer';
 
 }

 
 // Load all views
 
 $this->load->view('truelove_view/templates/header');
 
 $this->load->view('truelove_view/'.$page);    
  $this
->load->view('truelove_view/templates/'.$footer_template);


Also, just a small note, you don't need to include .php file ending.
Ah, that's a useful tip, thank you