File "exam_subjects.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/helper/exam_subjects.php
File size: 1.47 KiB (1507 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

<?php
require_once '../../config/config.php';
header('Content-Type: application/json');

try {
    $examId = isset($_GET['exam_id']) ? (int)$_GET['exam_id'] : 0;
    $examSubjectId = isset($_GET['exam_subject_id']) ? (int)$_GET['exam_subject_id'] : 0;
    $onlyWithRegistrations = isset($_GET['with_registrations']) ? (int)$_GET['with_registrations'] === 1 : false;

    $params = [];
    $where = [];
    if ($examId > 0) { $where[] = 'es.exam_id = ?'; $params[] = $examId; }
    if ($examSubjectId > 0) { $where[] = 'es.exam_subject_id = ?'; $params[] = $examSubjectId; }

    $baseSql = "SELECT es.exam_subject_id, es.exam_id, e.exam_name,
                        s.subject_id, s.subject_code, s.subject_name
                 FROM exam_subjects es
                 JOIN exams e ON es.exam_id = e.exam_id
                 JOIN subjects s ON es.subject_id = s.subject_id";
    if ($onlyWithRegistrations) {
        $baseSql .= " JOIN student_registrations sr ON sr.exam_subject_id = es.exam_subject_id";
    }
    if ($where) { $baseSql .= ' WHERE ' . implode(' AND ', $where); }
    $baseSql .= ' GROUP BY es.exam_subject_id ORDER BY e.exam_name, s.subject_code';

    $stmt = $pdo->prepare($baseSql);
    $stmt->execute($params);
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(['status'=>'success','data'=>$rows]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager