Reputation: 5419
I have an array ($data) in my model. I would like to pass it to my controller, so the controller can pass it to the view.
Model:
$data['table'] = $db->get_custom_db($target)->list_tables();
return $data;
Controller:
$this->load->view('page', $data);
View:
var_dump($data); // This returns NULL
How do I do this?
Edit: This is the full code
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
public function index() {
$this->load->model('connection_model');
$db = new Connection_model();
if (isset($_SESSION['connection'])) {
if ($db->get_custom_db('sender') && $db->get_custom_db('receiver')) {
$this->load->model('readdata_model');
$readData = new ReadData_model();
$readData->get_table('sender');
$this->layout->load_template('tables', 'Data Migrator: Overview');
}
}
else {
// Load the template of Home
$this->layout->load_template('home', 'Data Migrator: Home');
}
}
}
Layout.php (libraries)
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Layout {
protected $ci;
public function __construct() {
$this->ci =& get_instance();
}
public function load_template($page, $title, $full_template = true, $model = NULL, $model_position = 'top') {
if (file_exists(APPPATH . 'views/pages/' . $page . '.php')) {
$data['page_title'] = $title;
if ($model_position != 'top' && $model_position != 'bottom') {
exit('Error: Bad parameter (' . $model_position . '). Please use "top" or "bottom" instead.');
}
// Building the template
if ($full_template) {
$this->ci->load->view('template/header', $data);
if ($model && $model_position == 'top') {
$model;
}
}
$this->ci->load->view('pages/' . $page, $data);
if ($model && $model_position == 'bottom') {
$model;
}
if ($full_template) {
$this->ci->load->view('template/footer');
}
}
else {
show_404();
}
}
}
Model
<?php
class ReadData_model extends CI_Model {
public function get_table($target) {
if ($target != 'sender' && $target != 'receiver') { exit('Error: Illegal parameter. Please use sender or receiver instead.'); }
$this->load->model('Connection_model');
$db = new Connection_model();
$data['table'] = $db->get_custom_db($target)->list_tables();
return $data;
}
}
View
<?php
var_dump($table);
Upvotes: 0
Views: 757
Reputation: 2998
In your view, don't try to print $data['index']
, but instead: $index
. That's how Codeigniter works.
So in your code, $table
should be accessible in the view, not $data['table']
.
Upvotes: 1