<?php
include('includes/dbconnection.php');
$student_id = isset($_GET['student_id']) ? intval($_GET['student_id']) : 0;

if ($student_id > 0) {
  $query = $con->prepare("
      SELECT subject_name, class, section, created_at 
      FROM subject_allocations 
      WHERE student_id = ? ORDER BY subject_name ASC
  ");
  $query->bind_param("i", $student_id);
  $query->execute();
  $result = $query->get_result();

  if ($result->num_rows > 0) {
    echo '<table class="table table-bordered table-sm">';
    echo '<thead class="bg-dark text-white"><tr>
            <th>#</th>
            <th>Subject Name</th>
            <th>Class</th>
            <th>Arm</th>
          </tr></thead><tbody>';
    $cnt = 1;
    while ($row = $result->fetch_assoc()) {
      echo "<tr>
              <td>{$cnt}</td>
              <td>" . htmlspecialchars($row['subject_name']) . "</td>
              <td>" . htmlspecialchars($row['class']) . "</td>
              <td>" . htmlspecialchars($row['section']) . "</td>
            </tr>";
      $cnt++;
    }
    echo '</tbody></table>';
  } else {
    echo '<div class="alert alert-warning">No subjects allocated to this student.</div>';
  }
} else {
  echo '<div class="alert alert-danger">Invalid student ID.</div>';
}
?>
