<?php

define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'restax');

define('DB_PASSWORD', 'restax@123');

define('DB_NAME', 'Restax'); 

$con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

$course_id = $request->getAttribute('course_id');
$dept_id = $request->getAttribute('dept_id');
$level_id = $request->getAttribute('level_id');
$year = $request->getAttribute('year');
$term = $request->getAttribute('term');
$type = $request->getAttribute('type');
$papers_shortcode = $request->getAttribute('papers_shortcode');

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$response=array();





if(isset($course_id)&&isset($dept_id)&&isset($level_id)&&isset($year)){
		$sql="SELECT set_papers.course_id, set_papers.dept_id, set_papers.level_id, set_papers.year, set_papers.term, set_papers.papers_shortcode, set_papers.paper_name, set_papers.credits, set_papers.type,  course.course_name, department.dept_name, department.dept_shortcode, level.level_name, level.level_shortcode FROM set_papers INNER JOIN course ON course.course_id=set_papers.course_id
		INNER JOIN department ON department.dept_id=set_papers.dept_id
		INNER JOIN level ON level.level_id=set_papers.level_id WHERE set_papers.course_id='$course_id' AND set_papers.dept_id='$dept_id' AND set_papers.level_id='$level_id' AND set_papers.year='$year'";
}
elseif(isset($course_id)&&isset($dept_id)&&isset($level_id)){
		$sql="SELECT set_papers.course_id, set_papers.dept_id, set_papers.level_id, set_papers.year, set_papers.term, set_papers.papers_shortcode, set_papers.paper_name, set_papers.credits, set_papers.type,  course.course_name, department.dept_name, department.dept_shortcode, level.level_name, level.level_shortcode FROM set_papers INNER JOIN course ON course.course_id=set_papers.course_id
		INNER JOIN department ON department.dept_id=set_papers.dept_id
		INNER JOIN level ON level.level_id=set_papers.level_id WHERE set_papers.course_id='$course_id' AND set_papers.dept_id='$dept_id' AND set_papers.level_id='$level_id'";
}
elseif(isset($course_id)&&isset($dept_id)){
		$sql="SELECT set_papers.course_id, set_papers.dept_id, set_papers.level_id, set_papers.year, set_papers.term, set_papers.papers_shortcode, set_papers.paper_name, set_papers.credits, set_papers.type,  course.course_name, department.dept_name, department.dept_shortcode, level.level_name, level.level_shortcode FROM set_papers INNER JOIN course ON course.course_id=set_papers.course_id
		INNER JOIN department ON department.dept_id=set_papers.dept_id
		INNER JOIN level ON level.level_id=set_papers.level_id WHERE set_papers.course_id='$course_id'AND set_papers.dept_id='$dept_id'";
}
elseif(isset($course_id)){
$sql="SELECT set_papers.course_id, set_papers.dept_id, set_papers.level_id, set_papers.year, set_papers.term, set_papers.papers_shortcode, set_papers.paper_name, set_papers.credits, set_papers.type,  course.course_name, department.dept_name, department.dept_shortcode, level.level_name, level.level_shortcode FROM set_papers INNER JOIN course ON course.course_id=set_papers.course_id
		INNER JOIN department ON department.dept_id=set_papers.dept_id
		INNER JOIN level ON level.level_id=set_papers.level_id  WHERE  set_papers.course_id='$course_id' ";
}
else{
		$sql="SELECT set_papers.course_id, set_papers.dept_id, set_papers.level_id, set_papers.year, set_papers.term, set_papers.papers_shortcode, set_papers.paper_name, set_papers.credits, set_papers.type,  course.course_name, department.dept_name, department.dept_shortcode, level.level_name, level.level_shortcode FROM set_papers INNER JOIN course ON course.course_id=set_papers.course_id
		INNER JOIN department ON department.dept_id=set_papers.dept_id
		INNER JOIN level ON level.level_id=set_papers.level_id";
}



$result=mysqli_query($con,$sql);

    while ($row[]=mysqli_fetch_assoc($result)) {  

      $user=$row;



    }



echo json_encode($user);




?>