File "read.php"

Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/valuation_duties/read.php
File size: 3 KiB (3074 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;
$staffId = isset($_GET['staff_id']) ? (int)$_GET['staff_id'] : 0;
$session = isset($_GET['session']) ? trim($_GET['session']) : '';

$columns = ['d.date', 'd.session', 'vr.room_name', "CONCAT(s.first_name,' ',s.last_name)", 'd.role'];
$orderBy = isset($columns[$orderColumn]) ? $columns[$orderColumn] : 'd.date';

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

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

if ($searchValue !== '') {
  $where[] = "(vr.room_name LIKE ? OR CONCAT(s.first_name,' ',s.last_name) LIKE ? OR d.role LIKE ?)";
  $params[] = "%$searchValue%"; $params[] = "%$searchValue%"; $params[] = "%$searchValue%";
}

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

$sql = "SELECT d.duty_id, d.date, d.session, d.role, d.room_id, d.staff_id,
               vr.room_name,
               CONCAT(s.first_name, ' ', s.last_name) AS staff_name
        FROM valuation_duties d
        JOIN valuation_rooms vr ON d.room_id = vr.room_id
        JOIN staff s ON d.staff_id = s.staff_id
        $whereClause
        ORDER BY $orderBy $orderDir
        LIMIT $start, $length";

$countSql = "SELECT COUNT(*) FROM valuation_duties d
             JOIN valuation_rooms vr ON d.room_id = vr.room_id
             JOIN staff s ON d.staff_id = s.staff_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 Duties Error: ' . $e->getMessage());
  echo json_encode(['draw'=>$draw,'recordsTotal'=>0,'recordsFiltered'=>0,'data'=>[], 'error'=>$e->getMessage()]);
}
?>

PHP File Manager