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


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

   //fetch the exact class name first 

   $sql = "SELECT name FROM classes WHERE id =:class_id";
   $conn->query($sql);
   $conn->bind(":class_id", $classID);

   $rowCount = $conn->rowCount();

   if($rowCount > 0){
       //display other details 

    $class_name = $conn->fetchColumn()
          //fetch student's data based on the specific class
    $sql = "SELECT * FROM results WHERE class = :classID ORDER BY subject_name";
    $conn->query($sql);
    $conn->bind(":classID", $classID);
    //generate HTML of student option
    if($conn->rowCount() > 0){
      $result = $conn->fetchMultiple();

      ?> <option value=''>Select Subject</option><?php
      foreach ($result as $subject) {
        $subject_name = $subject->subject_name;
          ?> <option value="<?php echo $subject->id; ?>"><?php echo $subject_name; ?></option><?php
      }
    }else{
        echo "<option value=''>Subject not found.</option>";
    }
   }



}else{
    echo "<option value=''>error occurred.</option>";
}