codeigniter ion auth getting user id

与世无争的帅哥 提交于 2019-12-23 09:06:50

问题


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);
    }

回答1:


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.




回答2:


you can retrieve the logged in user details using user() method.

For example to get user id

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


来源:https://stackoverflow.com/questions/17136142/codeigniter-ion-auth-getting-user-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!