<?php
session_start();
require_once '../admin/includes/database.php';
$conn = new Database();

if(isset($_POST['class_id']) && isset($_POST['section_id'])) {
    $classId = $_POST['class_id'];
    $sectionId = $_POST['section_id'];

    $sql = "SELECT studentID, name, registerNO FROM student 
            WHERE classesID = :class_id AND sectionid = :section_id 
            ORDER BY name ASC";
    $conn->query($sql);
    $conn->bind(':class_id', $classId);
    $conn->bind(':section_id', $sectionId);
    $students = $conn->fetchMultiple();

    if($conn->rowCount() > 0) {
        echo '<option value="">Select Student</option>';
        foreach($students as $student) {
            echo '<option value="'.$student->studentID.'">'.$student->name.' ('.$student->registerNO.')</option>';
        }
    } else {
        echo '<option value="">No students found</option>';
    }
} else {
    echo '<option value="">Invalid request</option>';
}