When a user logs in they are directed to my log in function — Here is my partially working code. I commented out the parts that I need help with.
model
if ($query->num_rows() == 1)
{
return $query->row()->f_name;
}
else {
return false;
}
}
controller
if($this->form_validation->run()) {
$f_name = $this->model_users->can_log_in();
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => 1,
'name' => $f_name
);
$this->session->set_userdata($data);
redirect('account_dashboard');
The user’s name is in the database table with their email/pw. I want to get the users f_name and set it in the session when they log in since the session is a global var.
view
my header shows their name:
echo $this->session->userdata('name');
How do I get db_values from the table and pass them along to the session?
When you call
$query->row(), it will return the first row from the query results as an object. So, if you need to return the user’s first name, which in this case is stored under thef_namefield, you’ll want to write this:Then in your controller, you’ll have the first name returned from the model function.
In case you need more than just the user’s first name in your controller, return the whole row from the model function instead.
Then in your, controller you can access the returned object (or array if you use
row_array()), by accessing its properties which will be named based on the fields’ name in the database table.It works the same way too for
row_array(), but of course, you’ll need to do something like$user['f_name']instead of$user->f_name.EDIT: Also, make sure the model is loaded by the controller, before you try to call its functions.