File "read.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/hall_settings/read.php
File size: 2.63 KiB (2697 bytes)
MIME-type: text/x-php
Charset: utf-8

Download   Open   Back

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

try {
    $isExport = isset($_GET['isExport']) && $_GET['isExport'] === 'true';
    $limit    = isset($_GET['length']) ? (int)$_GET['length'] : 10;
    $offset   = isset($_GET['start']) ? (int)$_GET['start'] : 0;
    $orderIdx = isset($_GET['order'][0]['column']) ? (int)$_GET['order'][0]['column'] : 0;
    $orderDir = (isset($_GET['order'][0]['dir']) && in_array(strtolower($_GET['order'][0]['dir']), ['asc','desc'])) ? $_GET['order'][0]['dir'] : 'asc';
    $search   = isset($_GET['search']['value']) ? trim($_GET['search']['value']) : '';

    $columns = [
        'h.hall_name',
        'hs.rows_count',
        'hs.columns_count',
        'hs.seating_method',
        'hs.bench_capacity',
        'hs.max_students',
        'hs.internal_invigilators_required',
        'hs.external_invigilators_required'
    ];
    $orderBy = isset($columns[$orderIdx]) ? $columns[$orderIdx] : 'h.hall_name';

    $where = '';
    $bindings = [];
    if ($search !== '') {
        $where = 'WHERE h.hall_name LIKE :q OR hs.seating_method LIKE :q';
        $bindings[':q'] = "%$search%";
    }

    $base = "FROM hall_settings hs JOIN halls h ON h.hall_id = hs.hall_id $where";
    $sql  = "SELECT hs.hall_setting_id, h.hall_name, h.capacity, hs.hall_id, hs.rows_count, hs.columns_count, hs.seating_method, hs.bench_capacity, hs.max_students, hs.row_labeling, hs.internal_invigilators_required, hs.external_invigilators_required $base ORDER BY $orderBy $orderDir";
    if (!$isExport) { $sql .= ' LIMIT :limit OFFSET :offset'; }

    $stmt = $pdo->prepare($sql);
    foreach ($bindings as $k=>$v) { $stmt->bindValue($k,$v, PDO::PARAM_STR); }
    if (!$isExport) { $stmt->bindValue(':limit',$limit, PDO::PARAM_INT); $stmt->bindValue(':offset',$offset, PDO::PARAM_INT); }
    $stmt->execute();
    $data = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $total = (int)$pdo->query('SELECT COUNT(*) FROM hall_settings')->fetchColumn();
    if ($where) {
        $stmtF = $pdo->prepare("SELECT COUNT(*) $base");
        foreach ($bindings as $k=>$v) { $stmtF->bindValue($k,$v, PDO::PARAM_STR); }
        $stmtF->execute();
        $filtered = (int)$stmtF->fetchColumn();
    } else { $filtered = $total; }

    if ($isExport) {
        echo json_encode(['data'=>$data,'export'=>true]);
    } else {
        echo json_encode([
            'draw' => (int)($_GET['draw'] ?? 0),
            'recordsTotal' => $total,
            'recordsFiltered' => $filtered,
            'data' => $data
        ]);
    }
} catch (Throwable $e) {
    http_response_code(500);
    echo json_encode(['error'=>'Server error: '.$e->getMessage()]);
}
?>


PHP File Manager