<?php
include("../../db.php");
header("Access-Control-Allow-Origin: * ");

$session = $_GET['session'];

$status = 0;
$msg = 'No data found';

$all_data = array();

if ($session != null && $session != ''){

    $all_data = $db->getRows("select * from assignment_new WHERE session = '$session'");

    $sub_new = $db->getRows("select * from subject_new WHERE session = '$session'");

    foreach ($all_data as $key=>$item){

        $all_data[$key]['parent_subject_name'] = $db->getVal("SELECT subject FROM subject_new WHERE id = '$item[parent_subject]' AND session ='$session'");
        $all_data[$key]['sub_subject_name'] = $db->getVal("SELECT subject FROM subject_new WHERE id = '$item[sub_subject]' AND session ='$session'");
        $all_data[$key]['chapter_name'] = $db->getVal("SELECT subject FROM subject_new WHERE id = '$item[chapter]' AND session ='$session'");

        $teacherName = $db->getVal("SELECT name FROM staff WHERE idno = '$item[added_by]'");
        
        $all_data[$key]['added_by_name'] = $teacherName ?:"NA";

    }

    $status = 1;
    $msg = "Query executed...";
}else{
    $msg = "Session is needed";
}

echo json_encode(array("status"=>$status, "data"=>$all_data, "msg"=>$msg));
