Passing data from controller into view
gierg opened this issue · 1 comments
gierg commented
I have been trying for passing data from the controller to the view, but the variable remains undefined.
My current code is as follows:
Home (controllers)
class Home extends MY_Controller {
public $global_data;
public function index()
{
$this->global_data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->render('home', 'full_width', $this->global_data);
}
}
Home (Views)
<div class="container-fluid">
<div class="row row-offcanvas row-offcanvas-left">
<?php $this->load->view('_partials/navbar-second'); ?>
<?php $this->load->view('_partials/home', $global_data); ?>
</div>
</div>
Error
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined variable: global_data</p>
<p>Filename: views/home.php</p>
<p>Line Number: 4</p>
<p>Backtrace:</p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\views\home.php<br>
Line: 4<br>
Function: _error_handler </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\third_party\MX\Loader.php<br>
Line: 357<br>
Function: include </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\third_party\MX\Loader.php<br>
Line: 300<br>
Function: _ci_load </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\views\_layouts\full_width.php<br>
Line: 3<br>
Function: view </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\third_party\MX\Loader.php<br>
Line: 357<br>
Function: include </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\third_party\MX\Loader.php<br>
Line: 300<br>
Function: _ci_load </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\core\MY_Controller.php<br>
Line: 240<br>
Function: view </p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\application\controllers\Home.php<br>
Line: 18<br>
Function: render
</p>
<p style="margin-left:10px">
File: D:\source\cimcp_manager\index.php<br>
Line: 315<br>
Function: require_once </p>
</div>
how am I going to do passing data?
buoncri commented
Render function have only two arguments. The data are going to view by the mViewData array. Look in the render method of the MY_Controller Object.
$this->mViewData['global_data'] = $this->global_data;
$this->render('home', 'full_width');
then you have $global_data array in view