ohctech_p8/abnormality_count.php
2024-10-16 19:18:52 +05:30

75 lines
2.0 KiB
PHP

<?php
include('includes/config/config.php');
include('includes/functions.php');
include('log_entry.php');
header('Content-Type: application/json');
// Single query to fetch all relevant data
$sql = "
SELECT
cpv.parameter_value_name,
d.dept_name,
COUNT(*) as count
FROM checkup_form_key_value cfkv
JOIN checkup_form cf ON cfkv.checkup_form_id = cf.checkup_id
JOIN patient_master pm ON cf.emp_id = pm.id
JOIN department d ON pm.dept_id = d.dept_id
JOIN checkup_parameter_value cpv ON cfkv.checkup_form_value = cpv.parameter_value_id
WHERE cfkv.checkup_form_key = 'ear_result'
GROUP BY cpv.parameter_value_name, d.dept_name
";
$result = $conn->query($sql);
// Initialize counts
$overallCounts = [
'Normal' => 0,
'Mild Hearing Loss' => 0,
'Moderate Hearing Loss' => 0,
'Severe Hearing Loss' => 0,
];
$departmentWiseCounts = [];
// Process the results
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$parameter_value_name = $row['parameter_value_name'];
$dept_name = $row['dept_name'];
$count = $row['count'];
// Update overall counts
if (isset($overallCounts[$parameter_value_name])) {
$overallCounts[$parameter_value_name] += $count;
}
// Initialize department counts if not already present
if (!isset($departmentWiseCounts[$dept_name])) {
$departmentWiseCounts[$dept_name] = [
'Normal' => 0,
'Mild Hearing Loss' => 0,
'Moderate Hearing Loss' => 0,
'Severe Hearing Loss' => 0,
];
}
// Update department-wise counts
if (isset($departmentWiseCounts[$dept_name][$parameter_value_name])) {
$departmentWiseCounts[$dept_name][$parameter_value_name] += $count;
}
}
}
$conn->close();
// Prepare the response
$response = [
'OverallCounts' => $overallCounts,
'DepartmentWiseCounts' => $departmentWiseCounts
];
// Return the response as JSON
echo json_encode($response);