Passing variable from controller to view in CodeIg

2019-01-12 02:36发布

问题:

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?

回答1:

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.



回答2:

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



回答3:

this is how to do it easily

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

Now in your view you can access the value this way

echo $planet;


回答4:

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 view you can access "$planet".That's it.accept answer if it is useful



回答5:

you can pass a variable in the url to

function regresion($value) {

    $data['value'] = $value;
    $this -> load -> view('cms/template', $data);
}

In the view

<?php print_r($value);?>


回答6:

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


回答7:

In view we pass array and the key of array will automatically become variable and will be made available to you by codeigniter, we can't simply pass variables to view we need to pass array

Below is your code

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

simply change it to

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

and in your view you can simple access it like this

echo $planet;