user1616244
user1616244

Reputation: 269

Passing variable from controller to view in CodeIgniter

I'm new to Codeigniter and OOP PHP.

Controller:

public function index(){
    $this->load->model('main_model');
    $planet = $this->main_model->solar();
    $this->load->view('main_view', $planet);    
    }

If echo $planet in the controller it does what it's supposed to do. If I echo $planet in the view I get an undefined variable error. $planet is not an array. Why isn't the $planet variable being passed to the view?

I know this is a simple and basic question and I'm embarrassed that I can't figure out what I'm doing wrong.

EDIT: Okay, after more fiddling around, I got it to work. Can variables only be passed from Controller to View when they're formatted as an array?

Upvotes: 21

Views: 88933

Answers (4)

GautamD31
GautamD31

Reputation: 28763

Try like:

public function index(){
    $this->load->model('main_model');
    $data['planet'] = $this->main_model->solar();
    $this->load->view('main_view', $data);    
}

and at your views you can access "$planet".

Upvotes: 1

jleft
jleft

Reputation: 3457

You can pass either an array or an object to the view. You can then access the array key(s) as a variable(s) in your view.

Controller

Array

public function index()
{
    $this->load->model('main_model');
    $planet_data['planet'] = $this->main_model->solar();
    $this->load->view('main_view', $planet_data);
}

Object

public function index()
{
    $this->load->model('main_model');
    $planet_data = new stdClass(); //Creates a new empty object
    $planet_data->planet = $this->main_model->solar();
    $this->load->view('main_view', $planet_data);
}

From CodeIgniter's user manual(deadlink): Note: If you use an object, the class variables will be turned into array elements.

View

Regardless of how you pass the data, it can be displayed like this:

<?php echo $planet; ?>

If it's an array then you would need to iterate through it. Or an object, then access it's member variables.


In my experience using an array is more common than using an object.

Upvotes: 13

Gaurav
Gaurav

Reputation: 721

If modal contain array response:

public function solar() {
   $data['earth'] = 'Earth';
   $data['venus'] = 'Venus';
   return $data;
}

then you the data to view like this:

public function index(){
    $this->load->model('main_model');
    $planet = $this->main_model->solar();
    $this->load->view('main_view', $planet);    
}

But at view you will have access data like this:

echo $earth;
echo $venus;

if you don't know want response will come then use code like this:

public function index(){
    $this->load->model('main_model');
    $planet['planet'] = $this->main_model->solar();
    $this->load->view('main_view', $planet);    
}

and in view file you can access the data like this:

print_r($planet);

If you don't know what data will come into view then just use this code at view:

print_r($this->_ci_cached_vars);

Upvotes: 1

Mischa
Mischa

Reputation: 43298

You have to pass an array to the view. CodeIgniter automatically makes $planet available to you.

$data = array('planet' => $planet);
$this->load->view('main_view', $data);

With this you can use $planet in your view.

E.g., if you do the following:

$data = array('foo' => 'Hello', 'bar' => 'world');
$this->load->view('main_view', $data);

$foo and $bar will be available in your view. The key-value pairs in the array are automatically converted to variables in the view.

Upvotes: 31

Related Questions