File "read.php"

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

Download   Open   Back

<?php
require_once '../../config/config.php';

$draw = isset($_GET['draw']) ? (int)$_GET['draw'] : 1;
$start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$length = isset($_GET['length']) ? (int)$_GET['length'] : 25;
$searchValue = isset($_GET['search']['value']) ? trim($_GET['search']['value']) : '';
$orderColumn = isset($_GET['order'][0]['column']) ? (int)$_GET['order'][0]['column'] : 0;
$orderDir = isset($_GET['order'][0]['dir']) ? $_GET['order'][0]['dir'] : 'asc';

$academicYear = isset($_GET['academic_year']) ? trim($_GET['academic_year']) : '';
$examTypeId = isset($_GET['exam_type_id']) ? (int)$_GET['exam_type_id'] : 0;
$date = isset($_GET['date']) ? trim($_GET['date']) : '';
$roomId = isset($_GET['room_id']) ? (int)$_GET['room_id'] : 0;
$session = isset($_GET['session']) ? trim($_GET['session']) : '';

$columns = ['t.date', 't.session', 'vr.room_name'];
$orderBy = isset($columns[$orderColumn]) ? $columns[$orderColumn] : 't.date';

$where = [];
$params = [];

if ($academicYear !== '') { $where[] = 't.academic_year = ?'; $params[] = $academicYear; }
if ($examTypeId > 0) { $where[] = 't.exam_type_id = ?'; $params[] = $examTypeId; }
if ($date !== '') { $where[] = 't.date = ?'; $params[] = $date; }
if ($session !== '') { $where[] = 't.session = ?'; $params[] = $session; }
if ($roomId > 0) { $where[] = 't.room_id = ?'; $params[] = $roomId; }

if ($searchValue !== '') {
  $where[] = "(vr.room_name LIKE ? OR t.session LIKE ?)";
  $params[] = "%$searchValue%"; $params[] = "%$searchValue%";
}

$whereClause = $where ? 'WHERE ' . implode(' AND ', $where) : '';

$sql = "SELECT t.timetable_id, t.date, t.session, t.room_id, t.academic_year, t.exam_type_id,
               vr.room_name
        FROM valuation_timetable t
        JOIN valuation_rooms vr ON t.room_id = vr.room_id
        $whereClause
        ORDER BY $orderBy $orderDir
        LIMIT $start, $length";

$countSql = "SELECT COUNT(*) FROM valuation_timetable t
             JOIN valuation_rooms vr ON t.room_id = vr.room_id
             $whereClause";

try {
  $stmt = $pdo->prepare($countSql);
  $stmt->execute($params);
  $total = (int)$stmt->fetchColumn();

  $stmt = $pdo->prepare($sql);
  $stmt->execute($params);
  $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

  echo json_encode([
    'draw' => $draw,
    'recordsTotal' => $total,
    'recordsFiltered' => $total,
    'data' => $rows
  ]);
} catch (Exception $e) {
  error_log('Read Valuation Timetable Error: ' . $e->getMessage());
  echo json_encode(['draw'=>$draw,'recordsTotal'=>0,'recordsFiltered'=>0,'data'=>[], 'error'=>$e->getMessage()]);
}
?>

PHP File Manager