File "find_existing.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/question_patterns/find_existing.php
File size: 1.79 KiB (1835 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

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

try {
    $subject_id  = (int)($_GET['subject_id'] ?? 0);
    $template_id = (int)($_GET['template_id'] ?? 0);

    if ($subject_id <= 0) {
        echo json_encode(['status' => 'error', 'message' => 'subject_id is required']);
        exit;
    }

    if ($template_id > 0) {
        $stmt = $pdo->prepare('SELECT qp.pattern_id, qp.pattern_name, qp.total_marks, s.subject_name, s.subject_code, t.template_name
                               FROM question_patterns qp
                               LEFT JOIN subjects s ON qp.subject_id = s.subject_id
                               LEFT JOIN question_pattern_templates t ON qp.template_id = t.template_id
                               WHERE qp.subject_id = ? AND qp.template_id = ?');
        $stmt->execute([$subject_id, $template_id]);
    } else {
        $stmt = $pdo->prepare('SELECT qp.pattern_id, qp.pattern_name, qp.total_marks, s.subject_name, s.subject_code
                               FROM question_patterns qp
                               LEFT JOIN subjects s ON qp.subject_id = s.subject_id
                               WHERE qp.subject_id = ?');
        $stmt->execute([$subject_id]);
    }

    $existing_pattern = $stmt->fetch(PDO::FETCH_ASSOC);

    if ($existing_pattern) {
        echo json_encode([
            'status' => 'found',
            'message' => 'Existing pattern found',
            'data' => $existing_pattern
        ]);
    } else {
        echo json_encode([
            'status' => 'not_found',
            'message' => 'No existing pattern found'
        ]);
    }

} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
?>

PHP File Manager