0

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);
3
  • Can you post full files? Commented Mar 10, 2015 at 4:24
  • can you post the block of code where you call model in controller? Commented Mar 10, 2015 at 9:14
  • I have updated my question. You can see the full code now. Commented Mar 10, 2015 at 16:38

1 Answer 1

1

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'].

Sign up to request clarification or add additional context in comments.

3 Comments

I also tried using $table. It still returns NULL and the error Undefined variable: table. I have updated my question. Please take a look at the whole code. I am sure the issue must be somewhere else.
What about $page_title, can you print it in your view?
Yes, I get the value of $page_title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.