Welcome Guest, Not a member yet? Register   Sign In
codeigniter passing data to inner include php
#1

Dear all,

I am using codeigniter to develop php web site. Now I have the question.
In controller, $this->load->view('index', $data);
I want $data can also share in header.php and footer.php that included in index.php.
It seems they cannot share and show undefined variables in header.php and footer.php.
How to do???
Reply
#2

In your controller you need to include the $data.
Use array $data and include it into your header and footer.

$data = array();

$this->load->view('header', $data);
$this->load->view('footer', $data);
Reply
#3

Or do it this way, make $data global to all views.

PHP Code:
$data = array();

$data['title'] = 'Title';

$this->load->vars($data);

$this->load->view('view_name'); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

Where would i add this code? I don't want to have to repeat all the data in every controller functions.
Reply
#5

Where and how do you load footer.ohp and header.php?
Reply
#6

(06-24-2015, 01:03 PM)iamthestreets Wrote: Where would i add this code? I don't want to have to repeat all the data in every controller functions.

Then create a MY_Controller
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

@InsiteFX

How do you make the variable GLOBAL in the MY_Controller.
Can you show us the complete codes.

sorry a little rusty here since I've been to Laravel.

Thanks in advance.
Reply
#8

Code:
class MY_Controller extends CI_Controller
{
    public $my_header;
    public $my_footer;      
}

then is welcome controller
class Welcome extends MY_Controller  
{
    function index()
    {
        $header = $this->my_header;
        // or echo $this->my_header in view;
    }
}
Reply




Theme © iAndrew 2016 - Forum software by © MyBB