Welcome Guest, Not a member yet? Register   Sign In
Help session grab user_id from database
#1

[eluser]the_unforgiven[/eluser]
Hi all,

I'm having major problems getting the data from from userdata even thought when i print_r the array i get this which clearly states the id is set:

Code:
Array ( [session_id] => f29e9f7a9facc6b8742e56a01446295d [ip_address] => 0.0.0.0 [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0 [last_activity] => 1337785533 [user_data] => [username] => test [is_user] => 1 [last_login] => 1337783842 [customer_id] => Array ( [id] => 14 [acc_number] => 172102 [ip_address] => 0

Basically i want to grab the id when each user is logged in and each user will have a different id obviously, but need to grab it from the session then store in a variable to use then within my controller: like so:

Code:
$data['cust_id'] = $this->session->userdata('id'); to pass to view or do whatever with

Also i this model
Code:
function getCustomer()
{
   $data = array();
   $options = array('id' => 14);
   $Q = $this->db->get_where('users',$options,1);
   if ($Q->num_rows() > 0){
        $data = $Q->row_array();
   }
     $Q->free_result();    
     return $data;
}
i just want to get the id associted with the username thats been logged in , how does one achive such thing?
#2

[eluser]Samus[/eluser]
[quote author="the_unforgiven" date="1337786168"]Hi all,

I'm having major problems getting the data from from userdata even thought when i print_r the array i get this which clearly states the id is set:

Code:
Array ( [session_id] => f29e9f7a9facc6b8742e56a01446295d [ip_address] => 0.0.0.0 [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:12.0) Gecko/20100101 Firefox/12.0 [last_activity] => 1337785533 [user_data] => [username] => test [is_user] => 1 [last_login] => 1337783842 [customer_id] => Array ( [id] => 14 [acc_number] => 172102 [ip_address] => 0

Basically i want to grab the id when each user is logged in and each user will have a different id obviously, but need to grab it from the session then store in a variable to use then within my controller: like so:

Code:
$data['cust_id'] = $this->session->userdata('id'); to pass to view or do whatever with

Also i this model
Code:
function getCustomer()
{
   $data = array();
   $options = array('id' => 14);
   $Q = $this->db->get_where('users',$options,1);
   if ($Q->num_rows() > 0){
        $data = $Q->row_array();
   }
     $Q->free_result();    
     return $data;
}
i just want to get the id associted with the username thats been logged in , how does one achive such thing?[/quote]
That would work, only thing is you have 'id' nested within an array ([customer_id]).

I'm not sure if this will work as it's off the top of my head, but you will either need to foreach it or access the array indexes directly. I don't know what i'm saying, but it'll look something along the lines of..

Code:
$data['cust_id'] = $this->session->userdata('customer_id');
// Then to get the id, do
$data['cust_id']['id'] // which should return '14'.
#3

[eluser]the_unforgiven[/eluser]
Unfortunatly that doesn't work Im afraid, to give you adds up on what am building might give you a better picture:

Building a small e-commerce which will allow customers to register and login so i need it grab the id then i can use that id to allow each unique customer to update their details so on and so forth.
#4

[eluser]Samus[/eluser]
[quote author="the_unforgiven" date="1337787646"]Unfortunatly that doesn't work Im afraid, to give you adds up on what am building might give you a better picture:

Building a small e-commerce which will allow customers to register and login so i need it grab the id then i can use that id to allow each unique customer to update their details so on and so forth.[/quote]
Then the problem is how you're storing the session data. Your id is being stored in a nested array, I don't see any reason it shouldn't just be stored naturally in the user_data array?

Wanna post some relevant code?
#5

[eluser]the_unforgiven[/eluser]
Controller to check login then store some session data:
Code:
function check()
{
  // Check user and password
  $query = $this->user_model->checkUser();
  
  $username = $this->input->post('username');
  $password = sha1($this->input->post('password'));
  
  $this->form_validation->set_rules('username', 'required|trim|max_length[15]');
  $this->form_validation->set_rules('password', 'required|trim|sha1');
    
  if ($this->form_validation->run() === FALSE) {
  
   $data['title'] = "Customer Login";
   $this->load->view('user/logon', $data);
  }
  else {
   if($query) {
    $data = array(
                 'username'    => $this->input->post('username'),
     'is_user'     => true,
     'last_login'  => time(),
     'customer_id' => $this->user_model->getCustomer(),
     'cust_id'     => $this->session->userdata('id')
              );
    
    $this->session->set_userdata($data);
    redirect('user/myaccount');
   }
  }
}

Model to getCustomer()
Code:
function getCustomer()
{
   $data = array();

   $Q = $this->db->get('users');
   if ($Q->num_rows() > 0){
        $data = $Q->row_array();
   }
     $Q->free_result();    
     return $data;
}
#6

[eluser]Samus[/eluser]
Code:
if($query) {
$result = $this->user_model->getCustomer();
foreach($result as $res) {
    $data = array(
                 'username'    => $this->input->post('username'),
     'is_user'     => true,
     'last_login'  => time(),
     'customer_id' => $res['id'], // assuming your db column name is 'id'
     'acc_number'  => $res['acc_number'],
     //'cust_id'     => $this->session->userdata('id') What's happening here? :S
              );
}

model
Code:
function getCustomer()
{
   $data = array();

   $Q = $this->db->get('users');
   if ($Q->num_rows() > 0){
        $data = $Q->result_array();
   }
     $Q->free_result();    
     return $data;
}

Let me know how that works
#7

[eluser]LuckyFella73[/eluser]
Ahh never mind, I mixed some posts ...
#8

[eluser]the_unforgiven[/eluser]

//'cust_id' => $this->session->userdata('id') What's happening here? :S
); was just playing whilst waiting for you to reply ealier Smile

Got this is my view file to see if worked $data['cust_id'] = $this->session->userdata('customer_id'); and just says array nothing else.

#9

[eluser]Samus[/eluser]
[quote author="the_unforgiven" date="1337788708"]
//'cust_id' => $this->session->userdata('id') What's happening here? :S
); was just playing whilst waiting for you to reply ealier Smile

Got this is my view file to see if worked $data['cust_id'] = $this->session->userdata('customer_id'); and just says array nothing else.

[/quote]
try print_r($cust_id) in your view.
#10

[eluser]the_unforgiven[/eluser]
from print_r in view i get this

Code:
Array ( [id] => 14 [acc_number] => 172102 [ip_address] => 0 [name] => Test

so the id is there right?




Theme © iAndrew 2016 - Forum software by © MyBB