如何将变量从控制器传递到视图 [英] How to pass variables from controller to view

查看:88
本文介绍了如何将变量从控制器传递到视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam使用codeigniter 2.2
以下是我的控制器类

iam using codeigniter 2.2 The following is my controller class

class Welcome extends CI_Controller {


public function index()
    {
     $test = "hello";
     $this->load->view('welcome_message',$test);
 }
}

以下是我的观点

<html><body><h1><?php echo  $data?></h1></body></html>

试图将变量传递给视图并不断出现此错误

trying to pass a variable to the view keep getting this error


遇到PHP错误

A PHP Error was encountered

严重性:通知

消息:未定义的变量:数据

Message: Undefined variable: data

文件名:views / welcome_message.php

Filename: views/welcome_message.php

行号:75


推荐答案

尝试一下,

class Welcome extends CI_Controller {


    public function index()
        {
            $test = "hello";
            $data['test'] = $test
            $this->load->view('welcome_message',$data);
        }
    }
}

视图中

< html>< body>< h1><?php echo $ test; ?>< / h1>< / body>< / html>

有关更多参考信息,请检查 https://ellislab.com/codeigniter/user-guide/general/views.html

For more reference check https://ellislab.com/codeigniter/user-guide/general/views.html

这篇关于如何将变量从控制器传递到视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆