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

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

	$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;

    echo json_encode($array);

?>