File "halls.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/seed/halls.php
File size: 1.8 KiB (1848 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

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

function ensureHall($pdo, $collegeId, $name, $block, $capacity){
    $sel = $pdo->prepare('SELECT hall_id FROM halls WHERE hall_name=? AND college_id=?');
    $sel->execute([$name, $collegeId]);
    $id = $sel->fetchColumn();
    if ($id) return (int)$id;
    $stmt = $pdo->prepare('INSERT INTO halls (college_id, hall_name, block_name, capacity) VALUES (?,?,?,?)');
    $stmt->execute([$collegeId, $name, $block, $capacity]);
    return (int)$pdo->lastInsertId();
}

try {
    // Find any college to attach halls to; seed academic structure first if needed
    $collegeId = (int)($pdo->query("SELECT college_id FROM colleges ORDER BY college_id LIMIT 1")->fetchColumn() ?: 0);
    if ($collegeId <= 0) {
        echo json_encode(['status'=>'error','message'=>'No college found. Seed academic_structure first.']);
        exit;
    }

    $defs = [
        ['Main Hall A', 'Block A', 120],
        ['Main Hall B', 'Block A', 100],
        ['Main Hall C', 'Block A', 90],
        ['Annex Hall D', 'Block B', 80],
        ['Annex Hall E', 'Block B', 70],
        ['Annex Hall F', 'Block B', 60],
        ['Lab Seminar G', 'Block C', 50],
        ['Auditorium H', 'Block D', 200],
        ['Lecture Hall I', 'Block E', 75],
        ['Lecture Hall J', 'Block E', 65]
    ];

    $created=0;$skipped=0; $ids=[];
    foreach ($defs as $d) { $id = ensureHall($pdo, $collegeId, $d[0], $d[1], $d[2]); if ($id) { $ids[]=$id; $created++; } else { $skipped++; } }

    echo json_encode(['status'=>'success','message'=>'Halls seeded','created'=>$created,'skipped'=>$skipped,'hall_ids'=>$ids]);
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['status'=>'error','message'=>$e->getMessage()]);
}
?>


PHP File Manager