php 将变量从控制器传递到 CodeIgniter 中的视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12294527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing variable from controller to view in CodeIgniter
提问by user1616244
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 $planetin the controller it does what it's supposed to do. If I echo $planetin the view I get an undefined variable error. $planetis not an array. Why isn't the $planetvariable being passed to the view?
如果echo $planet在控制器中它会做它应该做的事情。如果我echo $planet在视图中出现未定义的变量错误。 $planet不是数组。为什么不将$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?
编辑:好的,经过更多的摆弄,我开始工作了。当变量被格式化为数组时,变量只能从控制器传递到视图吗?
回答by Mischa
You have to pass an array to the view. CodeIgniter automatically makes $planetavailable to you.
您必须将数组传递给视图。CodeIgniter 会自动$planet提供给您。
$data = array('planet' => $planet);
$this->load->view('main_view', $data);
With this you can use $planetin your view.
有了这个,您可以$planet在您的视图中使用。
E.g., if you do the following:
例如,如果您执行以下操作:
$data = array('foo' => 'Hello', 'bar' => 'world');
$this->load->view('main_view', $data);
$fooand $barwill be available in your view. The key-value pairs in the array are automatically converted to variables in the view.
$foo并将$bar显示在您的视图中。数组中的键值对会自动转换为视图中的变量。
回答by jleft
You can pass either an arrayor an objectto 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.
来自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.
根据我的经验,使用数组比使用对象更常见。
回答by walecloud
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;
回答by Gaurav
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);
回答by Diogenes Chepe Ardines Salazar
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);?>
回答by Gautam3164
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
在您看来,您可以访问“$planet”。就是这样。如果有用,请接受答案
回答by Ali Haider
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;

