Welcome Guest, Not a member yet? Register   Sign In
Displaying session data into view?
#1

[eluser]anna16[/eluser]
Hi guys

I'm trying to display a data in view.

Here is the controller...
Code:
function login_account()
  {
        $this->data['username'] = $this->input->post('username');
        //store data to session.
        $this->session->set_userdata($this->data['username']);        
        
        //set form fields rules
    $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
    $this->form_validation->set_rules('username', 'Username', 'callback_username_check2');

        $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
        $this->form_validation->set_rules('password', 'Password', 'callback_password_check2');
        
        if ($this->form_validation->run() == FALSE) //display the login form again if invalid data is encountered.
        {
            $this->load->view('template/login-template', $this->data);            
        }
        else
        {
            $this->load->view('template/memberarea-template', $this->data);
        }
  }

Here is the view.
Code:
<table  bgcolor="#FFFFFF"  cellspacing="6">
<tr><td>

  <fieldset>
    <legend><b>Member Area</b></legend>
        &lt;?php echo "Welcome <b>", $this->session->userdata('username'); echo "</b> "; echo anchor('membership/site/login_form', 'Logout');?&gt;        
        <br><br><br><br><br><br><br><br><br><br>
  </fieldset>  
  
</td></tr>
</table>
<br>
&lt;?php #echo $username; ?&gt;<br>
&lt;?php #echo $password; ?&gt;<br>
<br><br>
&lt;?php echo validation_errors(); ?&gt;

I just really want to display the session username in the view.
But it doesn't display.

Thanks in advanced.
#2

[eluser]InsiteFX[/eluser]
controller:
Code:
function login_account()
{
    $data = array();

//set form fields rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check2');

//store data to session.
$data['username'] = $this->input->post('username');
$this->session->set_userdata('username', $this->input->post('username'));

  $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_password_check2');

//store data to session.
$data['password'] = $this->input->post('password');
$this->session->set_userdata('password', $this->input->post('password'));

  $this->load->vars($data);

if ($this->form_validation->run() == FALSE) //display the login form again if invalid data is encountered.
{
$this->load->view('template/login-template');
}
else
{
$this->load->view('template/memberarea-template');
}
}

view:
Code:
<table bgcolor="#FFFFFF" cellspacing="6">
<tr><td>

<fieldset>
<legend><b>Member Area</b></legend>
&lt;?php echo "Welcome <b>".$username."</b> ";?&gt;&lt;?php echo anchor('membership/site/login_form', 'Logout');?&gt;
<br><br><br><br><br><br><br><br><br><br>
</fieldset>

</td></tr>
</table>
<br>
&lt;?php echo $username; ?&gt;<br>
&lt;?php echo $password; ?&gt;<br>
<br><br>
&lt;?php echo validation_errors(); ?&gt;

InsiteFX
#3

[eluser]markup2go[/eluser]
Code:
&lt;?php #echo $username; ?&gt;<br>
&lt;?php #echo $password; ?&gt;<br>

The # isn't valid code. If you are developing you could open the main index.php file and have at the top:
Code:
ini_set('display_errors', 1);
error_reporting(E_ALL);

Or you can enable logging. I'm sure seeing the syntax error would have assisted you faster.

Also, you'll want to use the following syntax when passing single values for the session's userdata:

Code:
$this->session->set_userdata('some_name', 'some_value');

Or you can pass an array with keys/values.




Theme © iAndrew 2016 - Forum software by © MyBB