Codeigniter ion auth gets user id

I need to get a user ID to log in so that I can execute a request for this user. I use ion auth, should I use a session or call it from db, should I use a helper or not? Anyway, this is what I am trying to do:

example: how many work orders are registered with a user? query: select * from work_orders WHERE status = "1" AND userid = "% THE LOGGED IN USER"

here is my controller that does not have a "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); } 
+6
source share
2 answers

When a user logs in, you can get the user ID using the get_user_id() function in ion mode. In the appropriate note in your model, you need a call to $this->db->from() to set up a table to retrieve the number of rows.

 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 $this->ion_auth->logged_in() function to verify that the user is logged in before calling the model function.

+11
source

You can get the registered user data using the user () method.

For example, to get a user ID

 echo $this->ion_auth->user()->row()->id; 
+9
source

Source: https://habr.com/ru/post/947413/


All Articles