21

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?

4
  • 1
    The answers below are both correct. CodeIgniter's user guide is really useful. Adding Dynamic Data to the View on this page of the user guide is relevant to your question. Commented Sep 6, 2012 at 7:18
  • Variables can be passed from the controller to the view by way of an object or an array. Commented Sep 6, 2012 at 7:19
  • @user1616244 accept one of these answer Commented Sep 6, 2012 at 9:19
  • @jleft those hyperlinks seem to be dead. Commented Aug 16, 2020 at 23:51

4 Answers 4

31

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.

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

5 Comments

Is this also how variables need to passed from the Controller to the Model and from the Model back to the Controller?
No, you can just call a model function and save it in a variable. Like you are already doing in your code: $planet = $this->main_model->solar();
Thanks so much for all your help, guys. I know this was a pretty basic question. OOP PHP seems pretty easy, but learning and understanding the MVC model is a bit difficult for me.
@Mischa sir how this $data = array('foo' => 'Hello', 'bar' => 'world'); accessed in view page?
@KUMAR, it becomes a variable in the view, so $foo and $bar.
13

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.

1 Comment

i have read detailed guide about how to pass value from controller to view in codigniter cloudways.com/blog/how-to-pass-data-in-codeigniter
1

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);

Comments

1

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".

Comments

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.