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?

tereško
  • 58,060
  • 25
  • 98
  • 150
user1616244
  • 269
  • 2
  • 3
  • 8
  • 1
    The answers below are both correct. [CodeIgniter's user guide](http://codeigniter.com/user_guide/) is really useful. *Adding Dynamic Data to the View* on [this page](http://codeigniter.com/user_guide/general/views.html) of the user guide is relevant to your question. – jleft Sep 06 '12 at 07:18
  • Variables can be passed from the controller to the view by way of an *object* or an *array*. – jleft Sep 06 '12 at 07:19
  • @user1616244 accept one of these answer – Pramod Kumar Sharma Sep 06 '12 at 09:19
  • @jleft those hyperlinks seem to be dead. – mickmackusa Aug 16 '20 at 23:51

4 Answers4

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.

Mischa
  • 42,876
  • 8
  • 99
  • 111
  • Is this also how variables need to passed from the Controller to the Model and from the Model back to the Controller? – user1616244 Sep 06 '12 at 07:39
  • 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();` – Mischa Sep 06 '12 at 07:41
  • 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. – user1616244 Sep 06 '12 at 08:24
  • @Mischa sir how this `$data = array('foo' => 'Hello', 'bar' => 'world');` accessed in view page? – KUMAR Oct 03 '20 at 08:46
  • @KUMAR, it becomes a variable in the view, so `$foo` and `$bar`. – Mischa Oct 04 '20 at 12:21
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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
jleft
  • 3,457
  • 1
  • 23
  • 35
  • i have read detailed guide about how to pass value from controller to view in codigniter http://www.cloudways.com/blog/how-to-pass-data-in-codeigniter/ – Owais Alam Aug 21 '17 at 12:08
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".

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
GautamD31
  • 28,552
  • 10
  • 64
  • 85
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);
Gaurav
  • 721
  • 5
  • 14