<?php
// pages/get_batches.php
require_once __DIR__ . '/../includes/db.php';

$db = isset($mysqli) && $mysqli instanceof mysqli ? $mysqli
   : (isset($conn) && $conn instanceof mysqli ? $conn : null);
if (!$db) { http_response_code(500); echo '<option value="">-- Select Batch --</option>'; exit; }
$db->set_charset('utf8mb4');

$course_id = (int)($_GET['course_id'] ?? 0);

header('Content-Type: text/html; charset=utf-8');
echo '<option value="">-- Select Batch --</option>';
if ($course_id <= 0) exit;

// strictly by course_id, and show batch_name
$st = $db->prepare("SELECT id, batch_name FROM batches WHERE course_id=? ORDER BY batch_name ASC, id DESC");
$st->bind_param("i", $course_id);
$st->execute();
$rs = $st->get_result();
while ($rs && $r = $rs->fetch_assoc()) {
  $id = (int)$r['id'];
  $nm = htmlspecialchars($r['batch_name'], ENT_QUOTES, 'UTF-8');
  echo "<option value=\"$id\">$nm</option>";
}
$st->close();
