<?php
include("../db.php");

if (isset($_POST['sl_no'])) {
    $sl_no = mysqli_real_escape_string($con, $_POST['sl_no']);

    // Fetch category ID
    $cat_sql = mysqli_query($con, "SELECT cat_id FROM clg_course_cat WHERE sl_no = '$sl_no'");
    if ($cat_row = mysqli_fetch_assoc($cat_sql)) {
        $cat_id = $cat_row["cat_id"];
    } else {
        echo '<option value="">No Courses Found</option>';
        exit;
    }

    // Fetch courses
    echo '<option value="">Select From List</option>';
    $c_sql = mysqli_query($con, "SELECT sl_no, course_name FROM clg_course WHERE course_cat = '$cat_id'");
    while ($c_row = mysqli_fetch_assoc($c_sql)) {
        echo '<option value="' . $c_row['sl_no'] . '">' . $c_row['course_name'] . '</option>';
    }
}
?>