<?php
    $con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
  
    $items = array();
    $course=$request->getAttribute('course_id');
    $department=$request->getAttribute('dept_id');
    $level_id=$request->getAttribute('level_id');
    $batch=$request->getAttribute('batch');
    $execute='';

    if(!empty($course) && !empty($department) && !empty($level_id))
    {
	   $sql="SELECT DISTINCT batch from student WHERE course='$course'and department='$department' and level_id='$level_id'";
	   $execute='batch';
    }
    else if(!empty($course) && !empty($department))
    {
	   $sql="SELECT DISTINCT level.level_id,level.level_shortcode from course INNER JOIN student on course.course_id='$course' INNER JOIN department on department.dept_id='$department' INNER JOIN level on level.dept_id='$department'";
	   $execute='level';
	}
    else if(!empty($course))
    {
	    $sql="SELECT DISTINCT department.dept_shortcode,department.dept_id from department INNER JOIN student on student.course='$course' and department.course_id='$course'";
	    $execute='department';
	}
    else
    {
	    $sql="SELECT distinct course.course_id,course.course_name from course INNER JOIN student ";
	    $execute='course';
    }
    
    $result = $con->query($sql);
    if ($result->num_rows > 0) 
    {
    
    // output data of each row
        while($row = $result->fetch_assoc()) 
	    {
	        if($execute=='course')
	        {
		    $item[] = array("Course_id" => $row['course_id'],"Course" => $row['course_name'] );
	 	    }
	 	    else if($execute=='department')
	 	    {
	 	         $item[] = array("dept_shortcode" => $row['dept_shortcode'],
	 	                         "dept_id" => $row['dept_id']);
	 	         
	 	    }
	 	    else if($execute=='level')
	 	    {
	 	         $item[] = array("level_id" => $row['level_id'],"level_shortcode" => $row['level_shortcode']); 
	 	    }
	 	    else if($execute=='batch')
	 	    {
	 	           $item[] = array("batch"=> $row['batch']);
	 	    }

        }
	    $response=$item; 
        echo json_encode($response);
	} 
	else 
	{
     $response=array('status' => "false", 'Message' => "No Data");
            echo json_encode($response);
	}
?>