Codeigniter将数据从控制器传递到视图

时间:2022-02-28 12:51:46

As per here I'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变量的观点。我只是碰巧在控制器中使用相同的名称

6 个解决方案

#1


9  

Ah, the $data array's keys are converted into variables: try var_dump($title); for example.

啊,$ data数组的键被转换成变量:try var_dump($ title);例如。

EDIT: this is done using extract.

编辑:这是使用提取完成的。

#2


6  

you should do it like :

你应该这样做:

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

#3


4  

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] =>我的标题[标题] =>我的标题[消息] =>我的消息)

#4


1  

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

#5


0  

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;   

#6


0  

You can use this way also

你也可以用这种方式

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

#1


9  

Ah, the $data array's keys are converted into variables: try var_dump($title); for example.

啊,$ data数组的键被转换成变量:try var_dump($ title);例如。

EDIT: this is done using extract.

编辑:这是使用提取完成的。

#2


6  

you should do it like :

你应该这样做:

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

#3


4  

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] =>我的标题[标题] =>我的标题[消息] =>我的消息)

#4


1  

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

#5


0  

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;   

#6


0  

You can use this way also

你也可以用这种方式

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