I need to get the user id of the person logged in, so that i can run a query for that user. Im using ion auth, should i use a session or call it from the db, should i use a helper or not? anyway, heres what im trying to do:
example: how many work orders does logged in user have? query would be: select * from work_orders WHERE status = "1" AND userid = "%THE LOGGED IN USER"
here is my controller, which doesnt have the "get user id" code:
public function index()
{
$this->load->view('templates/header');
$data['total_open_wo'] = $this->Home_model->total_open_wo();
$data['result'] = $this->Home_model->index();
$this->load->view('home', $data);
$this->load->view('templates/footer');
}
here is my model:
public function total_open_wo() {
$this->db->where('status', "1");
$this->db->where('tid', "NEED_THE_USER_ID");
$num = $this->db->count_all_results('work_orders');
return ($num);
}
The default username for your ion router is (blank). The default password is admin. Enter the username & password, hit "Enter" and now you should see the control panel of your router.
Ion Auth is a simple and lightweight authentication library for the CodeIgniter framework.
Provided the user is logged in you can get the user ID by using the get_user_id()
function in ion auth. On a related note in your model you need a $this->db->from()
call to set the table to fetch the number of rows from.
public function total_open_wo() {
$userId = $this->ion_auth->get_user_id();
$this->db->where('status', '1');
$this->db->where('tid', $userId);
$this->db->from('work_orders');
$num = $this->db->count_all_results();
return $num;
}
Use the function $this->ion_auth->logged_in()
to make sure that the user is logged in before calling the model function.
you can retrieve the logged in user details using user() method.
For example to get user id
echo $this->ion_auth->user()->row()->id;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With