Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7421249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:22:34+00:00 2026-05-29T08:22:34+00:00

I am starting to learn codeigniter and the whole MVC framework. What im looking

  • 0

I am starting to learn codeigniter and the whole MVC framework. What im looking to find is how to add data to the header file without having to add it to every controller.

Here is my file layout

application
  --controller
    --main.php
  --view
    --includes
      --header.php
      --footer.php
      --template.php
    --main.php

In my main.php file i have

function main() {
    $data['mainView'] = "main";
    $this->load->view('includes/template',$data);
}

In my template.php file

<?PHP
 $this->load->view('includes/header');
 $this->load->view($mainView,$data);
 $this->load->view('includes/footer');

Again… what im looking for is a way to globally pass in data to the header file so I do not have to add the data to every controller i make. The type of data that i would be passing in is user data (user name, last login, messages….)

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:22:35+00:00Added an answer on May 29, 2026 at 8:22 am

    There are two good ways I’ve seen this done:

    1. Add it to a MY_Controller class, which all your related controllers extend:

    <?php
    
    class MY_Controller extends CI_Controller {
    
        public function __construct()
        {
            parent::__construct();
    
            $this->load->vars(array(
                'foo1' => 'bar1',
                'foo2' => 'bar2'
            ));
    
            // Now, all your views have $foo1 and $foo2
        }
    }
    

    2. Add it to a MY_Output class, which makes sense, as a full HTML page is rendered differently than, say, an AHAH partial or AJAX response:

    <?php
    
    class MY_Output extends CI_Output {
    
        public function _display($output = '')
        {
            if ($output == '')
            {
                $output = $this->final_output;
            }
    
            $CI =& get_instance();
    
            // Run checks here (on the Input class, likely) to see if the
            // response expects application/json, text/html, etc.
    
            $output = $CI->load->view('includes/header', array(
                'foo1' => 'bar1',
                'foo2' => 'bar2'
                ), TRUE) . $output;
    
            $output .= $CI->load->view('includes/footer', NULL, TRUE);
    
            parent::_display($output);
        }
    }
    

    This way also has the advantage of not having to include the header/footer on each of your views, and is easier to change directions later.

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm starting to learn ASP.Net MVC (the release candidate), and I'm having a little
So I'm starting to learn XML. It seems like a simple flat file data
I am starting to learn jQuery. Looking though the MVC3 project that makes use
I am starting to learn MVC(3) following a tutorial. I am a developer so
I'm starting to learn acceptance testing, and I want to test an ASP.NET MVC
Im starting to learn android programming and i was wondering where i could find
I am just starting to learn a bit about the entity framework and don't
just starting to learn Dojo, and I am having a heck of a time
I am just starting to learn Pyramid Framework . Is pyramid_who (repoze.who) the preferred
Just starting to learn the CakePHP framework. I'm using the Auth component, and all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.