Ricky
Ricky

Reputation: 568

How to get data of both table using join in codeigniter

I have two array

 Array
(
    [0] => stdClass Object
        (
            [id] => 14
            [employee_id] => ST0011
            [emp_name] => Munish Sharma
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 1970-01-01 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => fname
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

    [1] => stdClass Object
        (
            [id] => 12
            [employee_id] => ST001dasd
            [emp_name] => Rakesh Negi
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 2015-08-11 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => dsad
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

    [2] => stdClass Object
        (
            [id] => 13
            [employee_id] => ST001
            [emp_name] => Rakesh Negi
            [gender] => 
            [marital_status] => 
            [address] => 
            [postal_code] => 
            [home_phone] => 
            [mobile_phone] => 
            [work_email] => 
            [private_email] => 
            [joined_date] => 2015-08-06 00:00:00
            [emp_salary] => 0
            [confirmation_date] => 0000-00-00 00:00:00
            [department] => HRM
            [f_name] => Father Name
            [dob] => 1970-01-01
            [c_address] => 
            [domainName] => 
            [emp_status] => active
        )

)

and the second array is this

Array

(
    [0] => stdClass Object
        (
            [id] => 55
            [emp_id] => ST001
            [check_in] => 11:38:09
            [check_out] => 00:00:00
            [total_time] => 00:00:00
            [status] => 
            [date_time] => 2015-10-20
            [emp_name] => Rakesh Negi
        )

)

Now I want to make it like this

Array
    (
        [0] => stdClass Object
            (
                [id] => 14
                [employee_id] => ST0011
                [emp_name] => Munish Sharma
                [gender] => 
                [marital_status] => 
                [address] => 
                [postal_code] => 
                [home_phone] => 
                [mobile_phone] => 
                [work_email] => 
                [private_email] => 
                [joined_date] => 1970-01-01 00:00:00
                [emp_salary] => 0
                [confirmation_date] => 0000-00-00 00:00:00
                [department] => HRM
                [f_name] => fname
                [dob] => 1970-01-01
                [c_address] => 
                [domainName] => 
                [emp_status] => active
            )

        [1] => stdClass Object
            (
                [id] => 12
                [employee_id] => ST001dasd
                [emp_name] => Rakesh Negi
                [gender] => 
                [marital_status] => 
                [address] => 
                [postal_code] => 
                [home_phone] => 
                [mobile_phone] => 
                [work_email] => 
                [private_email] => 
                [joined_date] => 2015-08-11 00:00:00
                [emp_salary] => 0
                [confirmation_date] => 0000-00-00 00:00:00
                [department] => HRM
                [f_name] => dsad
                [dob] => 1970-01-01
                [c_address] => 
                [domainName] => 
                [emp_status] => active
            )

         [3] => stdClass Object
            (
                [id] => 55
                [emp_id] => ST001
                [check_in] => 11:38:09
                [check_out] => 00:00:00
                [total_time] => 00:00:00
                [status] => 
                [date_time] => 2015-10-20
                [emp_name] => Rakesh Negi
            )

)

I want to remove the value from first array if the em_id in the both array will be same is it possible in?

I tried using where condion with join like this

$date = date('Y-m-d');
            $this->db->select('hrm_attendance.*,employees.emp_name');
            $this->db->from('hrm_attendance');
            $this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');            
            $this->db->where('hrm_attendance.date_time = "'.$date.'"');
            $this->db->order_by('check_in','asc');
            $query = $this->db->get();
            return $query->result();

and getting this output

(
    [0] => stdClass Object
        (
            [id] => 55
            [emp_id] => ST001
            [check_in] => 11:38:09
            [check_out] => 00:00:00
            [total_time] => 00:00:00
            [status] => 
            [date_time] => 2015-10-20
            [emp_name] => Rakesh Negi
        )

)

But I want the output I have described above

if I remove $this->db->where('hrm_attendance.date_time = "'.$date.'"'); line then I am getting the all record from both the table I want all the data from the employee table with all ID and only those data from the hrm_attendance where the emp_id is available with where condition $this->db->where('hrm_attendance.date_time = "'.$date.'"');

Upvotes: 1

Views: 928

Answers (3)

Ricky
Ricky

Reputation: 568

I find out the solution by adding some code like this

            $date = date('Y-m-d');
            $this->db->select('hrm_attendance.*,employees.emp_name');
            $this->db->from('hrm_attendance');
            $this->db->join('employees','hrm_attendance.emp_id=employees.employee_id','LEFT OUTER');            
            $this->db->where('hrm_attendance.date_time = "'.$date.'"');
            $this->db->order_by('check_in','asc');
            $query = $this->db->get();
            $result = $query->result();
      $count = count($result);
       for($i = 0;$i<$count;$i++)
        {
               $id[] = $data['user_logged_in'][$i]->emp_id;
        }
            $this->db->select('*');
            $this->db->from('employees');
            $this->db->where_not_in('employee_id',$id);
            $query = $this->db->get();
            $data['absent'] = $query->result();
            print_r($data['absent']);

By using this I got the all those employee who are not in the table hrm_attendance

Upvotes: 1

user4419336
user4419336

Reputation:

In your db->where is incorrect $this->db->where('hrm_attendance.date_time = "'.$date.'"');

http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

public function some_name() {

   $date = date('Y-m-d');

   $this->db->select('*');
   $this->db->from($this->db->dbprefix . 'hrm_attendance ha', 'LEFT');           
   $this->db->join($this->db->dbprefix . 'employees e', 'e.employee_id = ha.emp_id', 'LEFT');            
   $this->db->where('ha.date_time', $date);
   $this->db->order_by('check_in','ASC');
   $query = $this->db->get();

   return $query->result();

   // Or Try With result_array();

   // return $query->result_array();

}

Upvotes: 1

Rana Soyab
Rana Soyab

Reputation: 896

Please check the Code igniter Docs

http://www.codeigniter.com/userguide2/database/active_record.html#select

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

// Produces: 
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

in select('*') you can specify your limited fields also.

Upvotes: 2

Related Questions