简体   繁体   English

Codeigniter 将数据从 controller 传递给查看

[英]Codeigniter passing data from controller to view

As per here I've got the following controller:根据这里,我有以下 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?我是否需要加载任何特定的模块/帮助程序来填充 $data 变量? If I print_r($this) , I can see a lot of stuff but none of my data except in caches如果我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.编辑:澄清一下,我知道在 controller 中调用相同的变量并且视图不会“共享”它 - 它不在 scope 但在我链接的示例中,它似乎暗示在 Z31A1FD140BEAECE18AD 中创建了一个$data变量的看法。 I simply happened to use the same name in the controller我只是碰巧在 controller 中使用了相同的名称

Ah, the $data array's keys are converted into variables: try var_dump($title);啊, $data数组的键被转换成变量:try var_dump($title); for example.例如。

EDIT: this is done using extract .编辑:这是使用extract完成的。

you should do it like:你应该这样做:

echo $title ;
echo $heading;
echo $message;

Or you can use it like array.或者你可以像数组一样使用它。 In Controller:在 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 )

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

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;   

You can use this way also你也可以用这种方式

 $data['data]=array('title'=>'value'); $this->load->view('view.php',$data);

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.在从 controller 发送数据以查看时,我们将其传递到数组中,并且 arrays 的键通过代码 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;

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

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