<?php
include("../../db.php");
//header("Access-Control-Allow-Origin: * ");
header('Content-Type: application/json');

    $class = $_REQUEST['class'];
    $subjectid = $_REQUEST['subject_id'];

    $session = "";

    if (isset($_REQUEST['session'])){
        $session = $_REQUEST['session'];
    }

	$array = array();
	$status = 0;

    $subSubjectArray = array();

	$allSubject = $db->getRows("select * from subject_new where class='$class' and parent_subject='".$subjectid."' and type='sub_subject'  ");
    if(count($allSubject) > 0){
        $status = 1;

    	foreach($allSubject as $subject){
    	    
    	    $chapterArray = array();
    	    
    	    	$allChapters = $db->getRows("select * from subject_new where class='$class' and parent_subject='".$subjectid."' and sub_subject='".$subject[id]."' and type='chapter' order by id ");
    	        if(count($allChapters) > 0){

                	foreach($allChapters as $chapter){
                            array_push($chapterArray,["id"=>"$chapter[id]","chapter"=>"$chapter[subject]" ]);
                	}
            
                }
                array_push($subSubjectArray,["id"=>"$subject[id]","sub_subject"=>"$subject[subject]", "chapter"=>$chapterArray ]);
    	}

    }


    $array['status'] = $status;
    $array['data'] = $subSubjectArray;

    $array['syllabus_covered'] = $db->getRows("select * from syllabus_covered where class='$class' and session = '$session' and subject_id='".$subjectid."'");

    echo json_encode($array);

?>