从控制器向“标题”视图发送值,但是当页面更改时,变量未定义

Sending the values of $data from controller to header but when page changes to another on which header is included it shows error.

Look at the code below

Controller

`$user_email=$_GET['email'];
  $this->load->model('Pmodel');

  $data['email']=$this->Pmodel->profile_model($user_email);

   $this->load->view('dashboard/profile',$data);`

now header "view"

    <?php  foreach ($data as $key ):?>
   <?php   
 $uname=$key['uname'];
 $user_email=$key['email'];
 ?>
    <li>
    <?php echo anchor('user/profile_user/?email='.$user_email, $uname ); ?></li>
  <?php endforeach; ?>

but when page changes from dashboard to profile it shows error A PHP Error was encountered

Severity: Notice

Message: Undefined variable: data

Filename: dashboard/header.php

Line Number: 58

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: dashboard/header.php

Line Number: 58

You can directly access the keys no loop required.

Like:

 <?php   
$uname=$uname;
$user_email=$email;
?>
<li>
  <?php 
      echo anchor('user/profile_user/?email='.$user_email, $uname;
  ?>
</li>

Try this ways..

use this code in your View

  <?php  foreach ($data as $key ):?>
   <?php   
 $uname=$key['uname'];
 $user_email=$key['email'];
 ?>
    <li>
    <?php echo anchor('user/profile_user/'.$user_email, $uname ); ?></li>
  <?php endforeach; ?>

and your controller use this code

public function profile_user($user_email)
{
   $data =array();
  $this->load->model('Pmodel');

  $data['email']=$this->Pmodel->profile_model($user_email);

   $this->load->view('dashboard/profile',$data);`
}

you send $data['email'] from controller, it means you send $email to view instead of$data

try this:

foreach ($email as $key )

you need to pass the data

 $data['email']=$this->Pmodel->profile_model($user_email);

in every controller and method .