CodeIgniter Forums
Session data - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Session data (/showthread.php?tid=51128)

Pages: 1 2 3


Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
How do I add information from the database to the session? I am new and trying to learn this. Sorry

I have a model with this info
Code:
function validate()
{
  $this->db->select('username', 'password', 'title');
  $this->db->where('username', $this->input->post('username'));
  $this->db->where('password', $this->input->post('password'));
  $query = $this->db->get('users');
  
  if($query->num_rows == 1)
  {

   return $query;
  }
  
}



And then a controller with this

Code:
public function validate_credentials(){  
  $this->load->model('membership_model');
  $query = $this->membership_model->validate();
  
   if($query) // if the user's credentials validated...
  
   {

    $data = array(
     'username' => $this->input->post('username'),
     'logged_in' => true
    );

    $this->session->set_userdata($data);
    $data['main_content'] = 'home';
    $data['page_title'] = 'title';
    $this->load->view('includes/template', $data);
   }else{  // incorrect username or password
    $this->index();
   }
  }

I am trying to add title to the session


Session data - El Forum - 04-21-2012

[eluser]InsiteFX[/eluser]
Code:
$data = array(
     'title'     => 'Your Title',
     'username'  => $this->input->post('username'),
     'logged_in' => true
);

// or
$this->session->set_userdata('title', 'your_title');



Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
I don't want to set the title there, i want to pull it from the database. It is already set there. The menu will changed based on the users title


Session data - El Forum - 04-21-2012

[eluser]InsiteFX[/eluser]
Well put it from your database and set it thats how you do it!



Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
Its in my database I dont know how to select it, or how to call it, please don't answer me like that. I need examples


Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
I selected it in the model, how to i retrieve it in the controller, or is that the wrong way. I need examples please.


Session data - El Forum - 04-21-2012

[eluser]Ajaxboy[/eluser]
Did you already went through the docs? http://ellislab.com/codeigniter/user-guide/libraries/sessions.html


Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
yes. I dont understand how to get the value of title into an array to add to session data


Session data - El Forum - 04-21-2012

[eluser]triplewhat[/eluser]
[quote author="InsiteFX" date="1335065627"]Well put it from your database and set it thats how you do it!
[/quote]

And how exactly do I do that Smile


Session data - El Forum - 04-21-2012

[eluser]Ajaxboy[/eluser]
I created my own session class so I am not making use of the CI session class..

but my understanding is that to get data you would do this:

$this->session->userdata('title');
$this->session->userdata('username');
$this->session->userdata('logged_in');