1

I am still quite new to Codeigniter and have a noob problem I need some help with.

I am trying to set the auto incremented id number for each user to the session data as user_id. eg:

Controller:

$username = $this->input->post('username');
$this->load->model('membership_model');
$user_id = $this->membership_model->getUser_id(); 

$data = array(
    'session_id' => $str = do_hash($str),
    'user_id' => $user_id,
    'username' => $username,
    'is_logged_in' => true
);          
$this->session->set_userdata($data);
redirect('site/members_area');

Model:

public function getUser_id()
{

    $session_username = $this->session->userdata('username');

    $q = $this->db->get_where('membership', array('username' => $session_username), 1);

    if($q->num_rows() > 0) {
        foreach($q->result() as $row) {
            $row->id;
            $row->username;
        }
        return $data;
    }
}

My problem is I am not sure how to pass the $row->id; object from the model to the controller to be used as the user_id.

Can someone please provide some advice or am I approaching this in the wrong way?

Thank you in advance for any assistance provided.

1
  • why not use an already existing security model for CI? There are plenty there. (I know this does not address your question tho). Commented Oct 4, 2011 at 4:54

1 Answer 1

1

Its quite simple, just return the id:

public function getUser_id()
{

    $session_username = $this->session->userdata('username');

    $q = $this->db->get_where('membership', array('username' => $session_username), 1);

    if($q->num_rows() > 0) {
       $row = $q->row(); 
       return $row->id;
    }
    return false;  // else return false
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.