=str_to_date('" . $start_date . "','%d-%m-%Y') "; } if (isset($end_date) && $end_date != '') { $qry_date1 .= " and date(medical_entry_date) <=str_to_date('" . $end_date . "','%d-%m-%Y') "; } if (isset($month) && $month != '') { $qry_date1 .= " and month(medical_entry_date) in (" . $month . ")"; } $i = 0; $record = array(); if (isset($year) && $year != '' && sizeof($year1) > 1) { $dept_query = "select dept_id, dept_name from department $qry_filter1"; $result_dept = mysqli_query($conn, $dept_query); $percentage_emp_per_dept = 0; $total = 0; while ($dept_row = mysqli_fetch_assoc($result_dept)) { for ($j = 0; $j < sizeof($year1); $j++) { $qry_date = ""; $qry_doj = ""; $qry_date .= " and year(medical_entry_date) ='" . $year1[$j] . "'"; $qry_doj .= " and year(doj) <= '" . $year1[$j] . "'"; $count_query = "select count(*) as total FROM patient_master where status = 'Active' and dept_id = '" . $dept_row['dept_id'] . "' $qry_filter $qry_doj"; $count_result = mysqli_query($conn, $count_query); error_log("count_query::" . $count_query); $row_count = mysqli_fetch_assoc($count_result); $count_bmi = "select max(medical_exam_id),patient_id from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and dept_id = '" . $dept_row['dept_id'] . "' $qry_filter $qry_doj ORDER by id asc) $qry_date1 $qry_date and (smoking='1' or alcohol='1' or tobacco='1') GROUP BY patient_id ORDER BY patient_id asc"; $bmi_result = mysqli_query($conn, $count_bmi); error_log("count_bmi::" . $count_bmi); if (mysqli_num_rows($bmi_result)) { $total = mysqli_num_rows($bmi_result); } else { $total = 0; } error_log("ftotal:" . $total); if ($total == 0 || $row_count['total'] == 0) { $record[$dept_row['dept_name']][$year1[$j]]['bmi'] = 0.00; error_log("habit per:" . $record[$dept_row['dept_name']][$year1[$j]]['bmi']); } else { $percentage_emp_per_dept = ($total / $row_count['total']) * 100; $record[$dept_row['dept_name']][$year1[$j]]['bmi'] = round($percentage_emp_per_dept, 2); error_log("habit per:" . $record[$dept_row['dept_name']][$year1[$j]]['bmi']); } } } } else { if (isset($year2) && $year2 != '') { $qry_date .= " and year(medical_entry_date) ='" . $year2 . "'"; $qry_filter .= " and year(doj) <= '".$year2."'"; } $dept_query = "select dept_id, dept_name from department $qry_filter1"; $result_dept = mysqli_query($conn, $dept_query); $total = 0; while ($dept_row = mysqli_fetch_assoc($result_dept)) { $count_query = "select count(*) as total FROM patient_master where status = 'Active' and dept_id = '" . $dept_row['dept_id'] . "' $qry_filter"; $count_result = mysqli_query($conn, $count_query); error_log("count_query::" . $count_query); if (mysqli_num_rows($count_result) > 0) { $row_count = mysqli_fetch_assoc($count_result); $record[$i]['key'] = $dept_row['dept_name']; $record[$i]['total'] = $row_count['total']; } $count_bmi = "select max(medical_exam_id),patient_id from medical_examination where patient_id in (SELECT id FROM `patient_master` where status='Active' and dept_id = '" . $dept_row['dept_id'] . "' $qry_filter ORDER by id asc) $qry_date1 $qry_date and (smoking='1' or alcohol='1' or tobacco='1') GROUP BY patient_id ORDER BY patient_id asc"; $bmi_result = mysqli_query($conn, $count_bmi); error_log("count_bmi::" . $count_bmi); if (mysqli_num_rows($bmi_result) > 0) { $total = mysqli_num_rows($bmi_result); } else { $total = 0; } $record[$i]['bmi_total'] = $total; $i++; } } echo json_encode($record);