<?php
session_start();
include 'includes/database.php';
include 'includes/functions.php';
$conn = new Functions();

if (!empty($_POST['className'])) {
    $className = $_POST['className'];

    // Get class ID first
    $sql = "SELECT id FROM classes WHERE name = :class_name";
    $conn->query($sql);
    $conn->bind(':class_name', $className);
    $class_id = $conn->fetchColumn();

    if ($class_id) {
        $sql = "SELECT * FROM sections WHERE class = :class_id ORDER BY name";
        $conn->query($sql);
        $conn->bind(':class_id', $class_id);
        $sections = $conn->fetchMultiple();

        echo '<option value="">Select Section</option>';
        foreach ($sections as $section) {
            echo '<option value="' . $section->name . '">' . $section->name . '</option>';
        }
    } else {
        echo '<option value="">No sections found</option>';
    }
} else {
    echo '<option value="">Select class first</option>';
}