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

header('Content-Type: application/json');

  $class = $_REQUEST['class'];
  $id = $_REQUEST['id'];

    $status = 0;

    $subjectArray = array();
    
    $array = array();

	$allSubject = $db->getRows("select * from subject_new where class='$class' and parent_subject='".$id."' 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 sub_subject='".$subject[id]."' and type='chapter'  ");
    	    if(count($allChapters) > 0){
    	        foreach($allChapters as $chapter){
    	            array_push($chapterArray, ["id"=>"$chapter[id]", "name"=>"$chapter[subject]"]);
    	        }
    	    }
    	    
                array_push($subjectArray,["id"=>"$subject[id]","name"=>"$subject[subject]","chapters"=>json_encode($chapterArray) ]);
    	}

    }
 
//  $array['status'] = $status;
//  $array['subjects'] = $subjectArray;
 
 echo json_encode($subjectArray);
?>