Mini Shell

Direktori : /home/mhcadmin/public_html/Portal/
Upload File :
Current File : /home/mhcadmin/public_html/Portal/get_hod_by_department.php

<?php
session_start();
require_once 'config/config.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['department'])) {
    $department_id = mysqli_real_escape_string($conn, $_POST['department']);
    
    // Get department name from Department table
    $dept_query = "SELECT Name FROM Department WHERE id = '$department_id' AND Deleted = 'No'";
    $dept_result = mysqli_query($conn, $dept_query);
    $dept_name = '';
    
    if ($dept_result && mysqli_num_rows($dept_result) > 0) {
        $dept_data = mysqli_fetch_assoc($dept_result);
        $dept_name = $dept_data['Name'];
    }
    
    // Query to get HODs from the selected department
    $query = "SELECT id, Full_Name, Position FROM admin_accounts 
              WHERE HOD = 1 
              AND Status = 'Active' 
              AND UserDepartment = '$department_id'
              ORDER BY Full_Name";
    
    $result = mysqli_query($conn, $query);
    $hods = array();
    
    if ($result && mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $hods[] = array(
                'id' => $row['id'],
                'full_name' => $row['Full_Name'],
                'position' => $row['Position']
            );
        }
    }
    
    echo json_encode(array(
        'success' => true,
        'hods' => $hods,
        'department_name' => $dept_name,
        'total_hods' => count($hods)
    ));
} else {
    echo json_encode(array(
        'success' => false,
        'message' => 'Invalid request',
        'hods' => []
    ));
}
?>