Codeigniter transfers data from the controller for viewing

By here I have 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 file I do this:

 <?php print_r($data);?> 

that leads to:

 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 populate the $ data variable? If I print_r($this) , I can see a lot of things, but none of my data except caches

Edit: To clarify, I know that calling a variable in the controller and the view will not “separate” it - this is out of scope, but in the example I linked, it seems that the $data variable created within the view. I just used the same name in the controller

+6
source share
6 answers

Ah, the keys of the $data array are converted to variables: for example, var_dump($title); .

EDIT: This is done using extract .

+9
source

you should do it like:

 echo $title ; echo $heading; echo $message; 
+6
source

Or you can use it as an array. In the controller:

 ... $this->load->view('User_Login', array('data' => $data)); ... 

In view:

 <?php print_r($data);?> 

will show you an array ([title] => My Title [heading] => My Heading [message] => My Message)

+4
source

you can pass the variable in the url to

 function regresion($value) { $data['value'] = $value; $this -> load -> view('cms/template', $data); } 

In view

 <?php print_r($value);?> 
+1
source

You cannot print the $ data variable since it is an associative array .... you can print each element of the associative array ..... consider the following example.

Do not do the following:

 echo $data; 

Do the following:

 echo $title; echo $heading; echo $message; 
0
source

You can also use this method.

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

Source: https://habr.com/ru/post/889903/


All Articles