File "read.php"
Full path: /home2/santhos2/public_html/bharath-coe.firstmatrix.in.net/api/courses/read.php
File size: 2.54 KiB (2603 bytes)
MIME-type: text/x-php
Charset: utf-8
<?php
require_once '../../config/config.php';
try {
$isExport = isset($_GET['isExport']) && $_GET['isExport'] === 'true';
$limit = isset($_GET['length']) ? (int)$_GET['length'] : 10;
$offset = isset($_GET['start']) ? (int)$_GET['start'] : 0;
$orderColumnIndex = 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';
$searchValue = isset($_GET['search']['value']) ? trim($_GET['search']['value']) : '';
$columns = ['course_id','course_code','course_name','college_name','regulation_year','total_semesters','status'];
$orderBy = isset($columns[$orderColumnIndex]) ? $columns[$orderColumnIndex] : 'course_id';
$whereClause = '';
$bindings = [];
if (!empty($searchValue)) {
$whereClause = "WHERE c.course_code LIKE :search OR c.course_name LIKE :search OR col.college_name LIKE :search";
$bindings[':search'] = "%$searchValue%";
}
$sqlData = "SELECT c.course_id, c.course_code, c.course_name, col.college_name, c.regulation_year, c.total_semesters, c.status
FROM courses c
LEFT JOIN colleges col ON col.college_id = c.college_id
$whereClause ORDER BY $orderBy $orderDir";
if (!$isExport) { $sqlData .= " LIMIT :limit OFFSET :offset"; $bindings[':limit']=$limit; $bindings[':offset']=$offset; }
$stmt=$pdo->prepare($sqlData);
foreach($bindings as $k=>$v){ $stmt->bindValue($k,$v,is_int($v)?PDO::PARAM_INT:PDO::PARAM_STR); }
$stmt->execute();
$data=$stmt->fetchAll(PDO::FETCH_ASSOC);
$totalRecords=(int)$pdo->query("SELECT COUNT(*) FROM courses")->fetchColumn();
if($whereClause){
$stmtF=$pdo->prepare("SELECT COUNT(*) FROM courses c LEFT JOIN colleges col ON col.college_id=c.college_id $whereClause");
foreach($bindings as $k=>$v){ if($k!==':limit'&&$k!==':offset'){ $stmtF->bindValue($k,$v,PDO::PARAM_STR);} }
$stmtF->execute();
$totalFilteredRecords=(int)$stmtF->fetchColumn();
} else {
$totalFilteredRecords=$totalRecords;
}
if ($isExport) echo json_encode(["data"=>$data,"export"=>true]);
else echo json_encode(["draw"=>isset($_GET['draw'])?(int)$_GET['draw']:0,"recordsTotal"=>$totalRecords,"recordsFiltered"=>$totalFilteredRecords,"data"=>$data]);
} catch (Throwable $e) { http_response_code(500); echo json_encode(["error"=>"Server error: ".$e->getMessage()]); }
?>