Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter ion auth getting user id

Tags:

codeigniter

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);
    }
like image 441
bnelsonjax Avatar asked Jun 16 '13 17:06

bnelsonjax


People also ask

How to find user id in ion?

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.

What is Ion_auth?

Ion Auth is a simple and lightweight authentication library for the CodeIgniter framework.


2 Answers

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.

like image 53
Bad Wolf Avatar answered Oct 15 '22 06:10

Bad Wolf


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

For example to get user id

echo $this->ion_auth->user()->row()->id;
like image 30
Arun Unnikrishnan Avatar answered Oct 15 '22 08:10

Arun Unnikrishnan