<?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 subjects 
                WHERE class_id = :class_id 
                ORDER BY subject_name";
        $conn->query($sql);
        $conn->bind(':class_id', $class_id);
        $subjects = $conn->fetchMultiple();

        echo '<option value="">Select Subject</option>';
        foreach ($subjects as $subject) {
            echo '<option value="' . $subject->subject_name . '">' . $subject->subject_name . '</option>';
        }
    } else {
        echo '<option value="">No subjects found</option>';
    }
} else {
    echo '<option value="">Select class first</option>';
}