简体   繁体   English

将变量从控制器传递到 CodeIgniter 中的视图

[英]Passing variable from controller to view in CodeIgniter

I'm new to Codeigniter and OOP PHP.我是 Codeigniter 和 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.如果在控制器中echo $planet它会做它应该做的事情。 If I echo $planet in the view I get an undefined variable error.如果我在视图中echo $planet ,我会收到一个未定义的变量错误。 $planet is not an array. $planet不是数组。 Why isn't the $planet variable being passed to the view?为什么不将$planet变量传递给视图?

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?当变量被格式化为数组时,变量只能从控制器传递到视图吗?

You have to pass an array to the view.您必须将数组传递给视图。 CodeIgniter automatically makes $planet available to you. CodeIgniter 会自动使您可以使用$planet

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

With this you can use $planet in your view.有了这个,您可以在视图中使用$planet

Eg, 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. $foo$bar将在您的视图中可用。 The key-value pairs in the array are automatically converted to variables in the view.数组中的键值对会自动转换为视图中的变量。

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.来自CodeIgniter 的用户手册(死链接):注意:如果使用对象,类变量将变成数组元素。

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.根据我的经验,使用数组比使用对象更常见。

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;

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" .在您看来,您可以访问"$planet"

you can pass a variable in the url to 你可以在url中传递一个变量

function regresion($value) {

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

In the view 在视图中

<?php print_r($value);?>

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

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 在视图中我们传递数组并且数组的键将自动变为变量并且将由codeigniter提供给你,我们不能简单地传递变量来查看我们需要传递数组

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM