Rajan
Rajan

Reputation: 2425

How to pass id into foreach-loop to fetch all relevant records?

I want to pass where condition in my model method, So I want to pass all the emp_id i get in an array to a for each loop and want that in my model method so i can pass it as where condition.

The Scenario is: I have a table users which contains all the user identified my unique id called emp_id now I have a table called daily_data2 which has login and logouts of users with emp_id there as well. So i have mapped the emp_id of users with emp_id of daily_data2

This is my controller method:

 public function index()
 {
     $user_type = $this->session->userdata('user_type');
     if($user_type=="admin")
     {
      $users = $this->data['users'] = $this->user_m->get();
      foreach ($users as $user) 
      {
        $user->emp_id;
        $this->data['attendances'] = $this->attendance_m->all_login($user->$emp_id);
        $this->data['attendances_logouts'] = $this->attendance_m->all_logout($user->$emp_id);        
      }
        $this->data['subview'] = 'admin/dashboard/index';
        $this->load->view('admin/_layout_main',$this->data);

     }

and in my model methods i have two methods one fetches login array and another the logout array which i pass to view and there i calculate the total working hours of a users.

So now i want to pass the emp_id in my model method to get each users' login and logout dynamically.

this is model:

 public function all_login($emp_id)
{

      $this->db->where->('emp_id',$emp_id);
}

Upvotes: 0

Views: 865

Answers (1)

Pathik Vejani
Pathik Vejani

Reputation: 4491

Modify your functions (keep your backup):

<?php
public function index()
{
    $user_type = $this->session->userdata('user_type');
    if($user_type=="admin")
    {
        $users = $this->user_m->get();
        $all_empids = implode(',', $users['emp_id']);
        $data['all_empids'] = $all_empids;
        $this->data['subview'] = 'admin/dashboard/index';
        $this->load->view('admin/_layout_main',$this->data);
    }
}

public function get_login_or_logout($eid)
{
    $q = $this->db->query("SELECT entry FROM daily_data2 WHERE emp_id = $eid");
    if($q->num_rows() > 0) {
        $d = $q->row_array();
        $temp = $d['entry'];
        if($temp > 100) {
            return 'login';
        } else {
            return 'logout';
        }
    } else {
        return 'No data found';
    }
}

?>

In View:

<?php
$explode_empids = explode(',', $all_empids);
foreach ($explode_empids as $eid) {
    $check_login_or_logout = $this->user_m->get_login_or_logout($eid);
    echo $check_login_or_logout;
}
?>

Upvotes: 1

Related Questions