CodeIgniter Forums
PHP error passing variable into view? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: PHP error passing variable into view? (/thread-49362.html)



PHP error passing variable into view? - El Forum - 02-16-2012

[eluser]solid9[/eluser]
Hi guys I have this basic error,

Code:
A PHP Error was encountered
  Severity: Notice
  Message: Undefined variable: username
  Filename: views/header.php
  Line Number: 37

I just want to pass the $username into hear.php view file.

<b>controller:</b>
Code:
//log the user in
function login()
{
    $username = 'GOD';
    $this->data['header'] = $this->load->view('header', $username, TRUE);
    $this->data['midnav'] = $this->load->view('body_m_frontpage', null, TRUE);  
    $this->load->view('front_page', $this->data);
}

<b>view:</b>
Code:
<div class='wrapper-head'>
  <div class='logo'>
   <a href="http://barterswapping.com">
    <img name="logoholder" src="&lt;?php echo base_url('img/barterlogo.jpg'); ?&gt;" width="200" height="130" alt="barterswapping.com">
   </a>  
  </div>
  
  <div class='login' align="right">
   <table border="0" bgcolor="red"><tr><td>
       &lt;?php echo form_open("main/process_login");?&gt;
          &lt;?php echo form_input('identity', 'Username');?&gt;
     &lt;?php echo ' '; ?&gt;
          &lt;?php echo form_input('password', 'Password');?&gt;
          
        <label for="remember">Remember Me:</label>
          &lt;?php echo form_checkbox('remember', '1', FALSE);?&gt;
     &lt;?php echo ' '; ?&gt;
          &lt;?php echo form_submit('submit', 'Login');?&gt;
       &lt;?php echo form_close();?&gt;
     </td>
     <td>
          &lt;?php echo anchor('main/create_user', 'Register', 'title="Register"'); ?&gt;
     </td>
     <td>&nbsp;</td>
     <td>
          &lt;?php echo anchor('main/forgotpass', 'Forgot Password?', 'title="ForgotPassword"'); ?&gt;          
     </td>
     <td>
          &lt;?php echo anchor('main/lgout', 'Logout', 'title="Logout"'); ?&gt;          
     </td>
    </tr>
   </table>
   <table border="0" bgcolor="black">
    <tr>
     <td align="right">
       <b>
     &lt;?php echo $username; ?&gt;
     </b>
     </td>
   </tr>
   </table>
  </div>
  
  <div class='menu'>
   <div id="navcontainer">
    <ul>
    <li><a href="#">How it works?</a></li>
    <li><a href="#">How to trade?</a></li>
    <li><a href="#">Events</a></li>
    <li><a href="#">Donate</a></li>
    </ul>
   </div>  
  </div>
</div>

Can you guys help me pass the $username properly into view.

Thanks in advanced.






PHP error passing variable into view? - El Forum - 02-16-2012

[eluser]InsiteFX[/eluser]
Code:
//log the user in
function login()
{
    $username = 'GOD';
    $this->data['username'] = $username;
    $this->data['header'] = $this->load->view('header', $data, TRUE);
    $this->data['midnav'] = $this->load->view('body_m_frontpage', null, TRUE);  
    $this->load->view('front_page', $this->data);
}



PHP error passing variable into view? - El Forum - 02-16-2012

[eluser]solid9[/eluser]
@InsiteFX
thanks anyways but i fix it.

Code:
//log the user in
function login()
{
    $data_user['username'] = 'GOD';
    $this->data['header'] = $this->load->view('header', $data_user, TRUE);
    $this->data['midnav'] = $this->load->view('body_m_frontpage', null, TRUE);  
    $this->load->view('front_page', $this->data);
}

Now I know the variable should be in array.

[PROBLEM SOLVED]


PHP error passing variable into view? - El Forum - 02-16-2012

[eluser]msAdele[/eluser]
This seems a little complicated. Ha. Smile


PHP error passing variable into view? - El Forum - 02-16-2012

[eluser]solid9[/eluser]
To make it global,

Code:
//log the user in
function login()
{
    //$data_user['username'] = $this->input->post('identity');
$this->data['username'] = 'OKAY';
    $this->data['header'] = $this->load->view('header', $this->data, TRUE);
    $this->data['midnav'] = $this->load->view('body_m_frontpage', null, TRUE);  
    $this->load->view('front_page', $this->data);
}