php Codeigniter 将数据从控制器传递到视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6257736/
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
Codeigniter passing data from controller to view
提问by Basic
As per hereI've got the following controller:
根据这里我有以下控制器:
class User extends CI_Controller {
public function Login()
{
//$data->RedirectUrl = $this->input->get_post('ReturnTo');
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('User_Login', $data);
}
//More...
}
and in my User_Login.php
view file I do this:
在我的User_Login.php
视图文件中,我这样做:
<?php print_r($data);?>
which results in:
这导致:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: data
Filename: views/User_Login.php
Line Number: 1
Do I need to load any specific modules/helpers to get the $data variable populated? If I print_r($this)
, I can see a lot of stuff but none of my data except in caches
我是否需要加载任何特定的模块/帮助程序来填充 $data 变量?如果我print_r($this)
,我可以看到很多东西,但除了缓存中没有我的数据
Edit: To clarify, I know that calling the variable the same in the controller and view won't "share" it - it's out of scope but in the example I linked, it seems to imply a $data
variable is created in the scope of the view. I simply happened to use the same name in the controller
编辑:为了澄清,我知道在控制器和视图中调用相同的变量不会“共享”它 - 它超出范围但在我链接的示例中,它似乎暗示$data
在范围内创建了一个变量看法。我只是碰巧在控制器中使用了相同的名称
回答by Femi
回答by Nishant Jani
you should do it like :
你应该这样做:
echo $title ;
echo $heading;
echo $message;
回答by Andriy Leshchuk
Or you can use it like array. In Controller:
或者你可以像数组一样使用它。在控制器中:
...
$this->load->view('User_Login', array('data' => $data));
...
In View:
在视图中:
<?php print_r($data);?>
will show you the Array ( [title] => My Title [heading] => My Heading [message] => My Message )
将显示数组( [title] => My Title [heading] => My Heading [message] => My Message)
回答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 ilankumaran k ilankumaran k
You can't print the variable $data as it is an associative array....you may print every element of the associative array.....consider the following example.
您不能打印变量 $data 因为它是一个关联数组....您可以打印关联数组的每个元素.....考虑以下示例。
Don't do as follows:
不要做以下事情:
echo $data;
Do as follows:
执行以下操作:
echo $title;
echo $heading;
echo $message;
回答by Chintan7027
You can use this way also
你也可以用这种方式
$data['data]=array('title'=>'value');
$this->load->view('view.php',$data);
回答by Ali Haider
while sending data from controller to view we pass it in array and keys of that arrays are made into variables by code codeigniter and become accessible in view file.
在从控制器发送数据以查看时,我们将其传递到数组中,该数组的键由代码 codeigniter 制成变量,并在视图文件中可访问。
In your code below all keys will become variables in User_Login.php
在下面的代码中,所有键都将成为 User_Login.php 中的变量
class User extends CI_Controller {
public function Login()
{
$data = array(
'title' => 'My Title', //In your view it will be $title
'heading' => 'My Heading', //$heading
'message' => 'My Message' //$message
);
$this->load->view('User_Login', $data);
}
}
and in your User_Login.php view you can access them like this:
在您的 User_Login.php 视图中,您可以像这样访问它们:
echo $title;
echo $heading;
echo $message;