DayDreaming
DayDreaming

Reputation: 99

How to pass variables from controller to view in CodeIgniter 3

I'm trying to pass some variables to my header.php in CodeIgniter 3.

I'm not sure but maybe there's an error because the header.php is not directly inside the pages folder and doesn't get passed?

hierarchy:

home.php --> \www\pages\home.php

header.php --> \www\pages\templates\home.php

I know that you can easily pass variables by loading the view:

Pages.php:
$data['title'] = "dashboard";

$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer');
home.php:
<?php print_r($title); ?> //WORKS
header.php:
<?php print_r($title); ?> //UNDEFINIED VARIABLE: title

Upvotes: 0

Views: 152

Answers (1)

Buddhimal Gunasekara
Buddhimal Gunasekara

Reputation: 76

Place your all views inside www\Project_folder\application\views path. Then you can pass variables like easily.

$data['title'] = "dashboard";

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

No need to pass $data variable to both header and home views. You can pass it to header only.

Upvotes: 1

Related Questions