File "read.php"

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

Download   Open   Back

<?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 = ['batch_id','batch_name','start_year','end_year','description','status'];
    $orderBy = isset($columns[$orderColumnIndex]) ? $columns[$orderColumnIndex] : 'batch_id';

    $whereClause = '';
    $bindings = [];
    if (!empty($searchValue)) {
        $whereClause = "WHERE batch_name LIKE :search OR description LIKE :search OR start_year LIKE :search OR end_year LIKE :search";
        $bindings[':search'] = "%$searchValue%";
    }

    $sqlData = "SELECT batch_id, batch_name, start_year, end_year, description, status FROM batches $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 batches")->fetchColumn();
    if($whereClause){ $stmtF=$pdo->prepare("SELECT COUNT(*) FROM batches $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()]); }
?>










PHP File Manager