<?php

/*Student API*/


/*Fetching API*/


/* Attempt to connect to MySQL database */
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'Restax_final');

define('DB_PASSWORD', 'Restax_final123');

define('DB_NAME', 'Restax_api_data');  

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

// Check connection
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)){
		$sql="SELECT grade_course.*,department.dept_shortcode,level.level_shortcode FROM grade_course INNER JOIN department ON department.dept_id = grade_course.dept_id INNER JOIN level ON level.level_id = grade_course.level_id WHERE grade_course.course_id='$course_id' AND grade_course.dept_id='$dept_id' AND grade_course.level_id='$level_id'";
}
elseif(isset($course_id)&&isset($dept_id)){
		$sql="SELECT grade_course.*,department.dept_shortcode,level.level_shortcode FROM grade_course INNER JOIN department ON department.dept_id = grade_course.dept_id INNER JOIN level ON level.level_id = grade_course.level_id WHERE grade_course.course_id='$course_id'AND grade_course.dept_id='$dept_id'";
}
elseif(isset($course_id)){
		$sql="SELECT grade_course.*,department.dept_shortcode,level.level_shortcode FROM grade_course INNER JOIN department ON department.dept_id = grade_course.dept_id INNER JOIN level ON level.level_id = grade_course.level_id WHERE grade_course.course_id='$course_id'";
}
else{
		$sql="SELECT grade_course.*,department.dept_shortcode,level.level_shortcode FROM grade_course INNER JOIN department ON department.dept_id = grade_course.dept_id INNER JOIN level ON level.level_id = grade_course.level_id";
}


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

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

      $user=$row;

    }

    echo json_encode($user);



 

 



?>