How to pass variables from controller to view

霸气de小男生 提交于 2020-01-17 05:03:07

问题


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

The following is my view

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

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

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: views/welcome_message.php

Line Number: 75


回答1:


Try this,

class Welcome extends CI_Controller {


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

in view

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

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



来源:https://stackoverflow.com/questions/32474187/how-to-pass-variables-from-controller-to-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!