-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_company_statistics.php
70 lines (57 loc) · 2.56 KB
/
get_company_statistics.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
require_once('./database/connect.php');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: application/json');
$department = isset($_GET['department']) ? $_GET['department'] : "";
$batch = isset($_GET['batch']) ? $_GET['batch'] : "";
try {
// Fetch all distinct specializations
$stmtSpecializations = $conn->prepare("SELECT DISTINCT specialization FROM placed_students WHERE department = :department AND batch = :batch");
$stmtSpecializations->bindParam(':department', $department, PDO::PARAM_STR);
$stmtSpecializations->bindParam(':batch', $batch, PDO::PARAM_STR);
$stmtSpecializations->execute();
$specializations = $stmtSpecializations->fetchAll(PDO::FETCH_COLUMN);
// Prepare the SQL statement to fetch company details and student count per specialization
$stmtCompanies = $conn->prepare("
SELECT
companyName,
category,
specialization,
COUNT(*) as numStudents
FROM placed_students
WHERE department = :department
AND batch = :batch
GROUP BY companyName, category, specialization
");
// Bind the department parameter to the query
$stmtCompanies->bindParam(':department', $department, PDO::PARAM_STR);
$stmtCompanies->bindParam(':batch', $batch, PDO::PARAM_STR);
// Execute the query
$stmtCompanies->execute();
// Fetch the result
$companies = $stmtCompanies->fetchAll(PDO::FETCH_ASSOC);
// Initialize an array to store the final result
$result = [];
// Process the result to group by company and category
foreach ($companies as $row) {
$companyName = $row['companyName'];
$category = $row['category'];
$specialization = $row['specialization'];
$numStudents = $row['numStudents'];
if (!isset($result[$companyName])) {
$result[$companyName] = [];
}
if (!isset($result[$companyName][$category])) {
$result[$companyName][$category] = [
'specializations' => array_fill_keys($specializations, '0') // Initialize all specializations with '0'
];
}
$result[$companyName][$category]['specializations'][$specialization] = $numStudents;
}
// Output the result as JSON
echo json_encode(['status' => 'success', 'company-statistics' => $result]);
} catch (PDOException $e) {
echo json_encode(['status' => 'error', 'message' => 'Error: ' . $e->getMessage()]);
}