<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
session_start(); 
include 'connect.php';

## Read value

$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
if($columnIndex == 1)
{
$columnName ='collegemid';
}else{
	
	$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
}
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = $_POST['search']['value']; // Search value

## Search 
$searchQuery = " ";
if($searchValue != ''){
	$searchQuery = " and (res_course_master.cmpbroucher like '%".$searchValue."%'  or res_course_master.cprogramlanguage like '%".$searchValue."%'  or  res_course_master.cmduration like '%".$searchValue."%'  or res_course_master.ctutionfees like '%".$searchValue."%'  or res_course_master.cprogramcategory like '%".$searchValue."%'  or res_course_master.cprogramdelivery like '%".$searchValue."%'  or res_course_master.costofliving like '%".$searchValue."%'  or res_course_master.levelofstudy in (select losmid from res_levelofstudy_master where levelofstudy like '%".$searchValue."%' ) or res_course_master.countrymid in (select countrymid from res_country_master where countryname like '%".$searchValue."%' ) or res_course_master.cnmid in (select cnmid from res_coursename_master where coursename like '%".$searchValue."%' ) or res_course_master.collegemid in (select collegemid from res_college_master where cmname like '%".$searchValue."%' )  
	                      ) ";
}


$aa="";
$aa1="";
//echo print_r($_SESSION['countrymid']);
	if(isset($_SESSION['countrymid']))
	{
		 $countrymid = $_SESSION['countrymid'];
		
		

		if("All" == $countrymid)
		{
		$aa="";
		}
		else
		{
			 $aa=" and res_course_master.countrymid = $countrymid ";
			 $aa1=" and countrymid = $countrymid ";
		}
	}
	
	$bb="";
	$bb1="";
	if( isset($_SESSION['cnmid']))
	{
		$cnmid = $_SESSION['cnmid'];
		
		if($cnmid == 'All')
		{
		
		}
		else
		{
				
			 $bb=" and res_course_master.cnmid = $cnmid";
			 $bb1=" cnmid = $cnmid";
		}
	}
	$cc='';
	if( isset($_SESSION['collegemid']))
	{
		$collegemid = $_SESSION['collegemid'];
		
		if($collegemid == 'All')
		{
		
		}
		else
		{
				
			 $cc=" and res_course_master.collegemid = '$collegemid'";
			 
		}
	}
		$dd='';
	if( isset($_SESSION['cprogramavailibilty']))
	{
		$cprogramavailibilty = $_SESSION['cprogramavailibilty'];
		
		if($cprogramavailibilty == 'All')
		{
		
		}
		else
		{
				
			 $dd=" and res_course_master.cprogramavailibilty = '$cprogramavailibilty'";
			 
		}
	}
		$aa2='';
if( isset($_SESSION['cintakestart']))
	{
		$cintakestart = $_SESSION['cintakestart'];
		
		if($cintakestart == 'All')
		{
		
		}
		else
		{
				
			 $aa2=" and res_course_master.coursemid in (select coursemid from res_courseintaketerm_detail where ctermmonth = '$cintakestart')";
			 
		}
	}
	$aa8='';
if( isset($_SESSION['ctermyear']))
	{
		$ctermyear = $_SESSION['ctermyear'];
		
		if($ctermyear == 'All')
		{
		
		}
		else
		{
				
			 $aa8=" and res_course_master.coursemid in (select coursemid from res_courseintaketerm_detail where ctermyear = '$ctermyear')";
			 
		}
	}
		$aa3='';
	if( isset($_SESSION['cprogramlength']))
	{
		$cprogramlength = $_SESSION['cprogramlength'];
		
		if($cprogramlength == 'All')
		{
		
		}
		else
		{
				
			 $aa3=" and res_course_master.cprogramlength = '$cprogramlength'";
			 
		}
	}
	
	if( isset($_SESSION['collegetypemid']))
	{
		$collegetypemid = $_SESSION['collegetypemid'];
		
		if($collegetypemid == 'All')
		{
		
		}
		else
		{
				
			 $aa3=" and res_course_master.collegemid in (select collegemid from res_college_master where collegetypemid = '$collegetypemid')";
			 
		}
	}
	$aa6='';
	if( isset($_SESSION['campusmid']))
	{
		$campusmid = $_SESSION['campusmid'];
		
		if($campusmid == 'All')
		{
		
		}
		else
		{
				
			 $aa6=" and res_course_master.campusmid = '$campusmid'";
			 
		}
	}
	$aa7='';
	if( isset($_SESSION['cbacklogaccepted']))
	{
		$cbacklogaccepted = $_SESSION['cbacklogaccepted'];
		
		if($cbacklogaccepted == 'All')
		{
		
		}
		else
		{
				
			 $aa7=" and res_course_master.cbacklogaccepted <= '$cbacklogaccepted'";
			 
		}
	}
	$aa9='';
	if( isset($_SESSION['clangscore']) && isset($_SESSION['clangeligibility']))
	{
		$clangscore = $_SESSION['clangscore'];
		$clangeligibility = $_SESSION['clangeligibility'];
		
		if($clangscore == 'All' && $clangeligibility == 'All')
		{
		
		}
		else
		{
				
			 $aa9=" and res_course_master.coursemid in (select coursemid from res_courselanguage_detail where clangscore >= $clangscore and clangeligibility='$clangeligibility')";
			 
		}
	}
	
	$aa10='';
	if( isset($_SESSION['clangscore1']) && isset($_SESSION['clangeligibility1']))
	{
		$clangscore1 = $_SESSION['clangscore1'];
		$clangeligibility1 = $_SESSION['clangeligibility1'];
		
		if($clangscore1 == 'All' && $clangeligibility1 == 'All')
		{
		
		}
		else
		{
				
			 $aa10=" and res_course_master.coursemid in (select coursemid from res_courselanguage_detail where clangscore >= $clangscore1 and clangeligibility='$clangeligibility1')";
			 
		}
	}
	
	$aa4='';
	if( isset($_SESSION['ctutionfees']))
	{
		$ctutionfees = $_SESSION['ctutionfees'];
		
		if($ctutionfees == 'Below 15000')
		{
			$aa4=" and res_course_master.ctutionfees < '$ctutionfees'";
		}
		else if($ctutionfees == '15000 - 20000')
		{
			$aa4=" and res_course_master.ctutionfees < 20000 and res_course_master.ctutionfees >= 15000";
		}else if($ctutionfees == '20000 - 25000')
		{
			$aa4=" and res_course_master.ctutionfees < 25000 and res_course_master.ctutionfees >= 20000";
		}else if($ctutionfees == '25000 - 30000')
		{
			$aa4=" and res_course_master.ctutionfees < 30000 and res_course_master.ctutionfees >= 25000";
		}else if($ctutionfees == 'Above 30000')
		{
			$aa4=" and  res_course_master.ctutionfees >= 30000";
		}
		
	}
	$aa5='';
	if( isset($_SESSION['capplicationfee']))
	{
		$capplicationfee = $_SESSION['capplicationfee'];
		
		if($capplicationfee == 'Below 50')
		{
			$aa5=" and res_course_master.capplicationfee < 50";
		}
		else if($capplicationfee == '50 - 100')
		{
			$aa5=" and res_course_master.capplicationfee < 100 and res_course_master.capplicationfee >= 50";
		}else if($capplicationfee == '100 - 150')
		{
			$aa5=" and res_course_master.capplicationfee < 150 and res_course_master.capplicationfee >= 100";
		}else if($capplicationfee == '150 - 200')
		{
			$aa5=" and res_course_master.capplicationfee < 200 and res_course_master.capplicationfee >= 150";
		}else if($capplicationfee == '200 - 300')
		{
			$aa5=" and res_course_master.capplicationfee < 300 and res_course_master.capplicationfee >= 200";
		}else if($capplicationfee == 'Above 300')
		{
			$aa5=" and  res_course_master.capplicationfee >= 300";
		}
		
	}
	

   /* $columnName = 'collegemid';
$columnSortOrder='asc';
$row=1;
$rowperpage=20;   
 */
## Total number of records without filtering
$sel = mysqli_query($conn,"select count(*) as allcount from res_course_master ");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];

## Total number of records with filtering
//echo $ewe= "select count(*) as allcount from res_course_master,res_coursename_master WHERE res_course_master.cnmid = res_coursename_master.cnmid $aa $bb $cc $dd $aa2 $aa3 $aa4 $aa5 $aa6 $aa7 $aa8 $aa9".$searchQuery ;
  $sel = mysqli_query($conn,"select count(*) as allcount from res_course_master,res_coursename_master WHERE res_course_master.cnmid = res_coursename_master.cnmid $aa $bb $cc $dd $aa2 $aa3 $aa4 $aa5 $aa6 $aa7 $aa8 $aa9 $aa10 ".$searchQuery );
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
//echo $ee = "select * from res_course_master,res_college_master where res_course_master.collegemid = res_college_master.collegemid and res_course_master.collegemid = res_college_master.collegemid $aa $bb $cc $dd $aa2 $aa3 $aa4 $aa5 $aa6 $aa7 $aa8 $aa9".$searchQuery."  order by res_course_master.".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
  $empQuery = "select * from res_course_master,res_college_master where res_course_master.collegemid = res_college_master.collegemid and res_course_master.collegemid = res_college_master.collegemid $aa $bb $cc $dd $aa2 $aa3 $aa4 $aa5 $aa6 $aa7 $aa8 $aa9 $aa10 ".$searchQuery."  order by res_course_master.".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
//$empQuery = "select * from res_inquiry_master WHERE 1 ";
$empRecords = mysqli_query($conn, $empQuery);
$data = array();
$i=1;
while ($rt = mysqli_fetch_assoc($empRecords)) {
	
	
	$coursemid=$rt['coursemid'];
				
				
				$collegemid=$rt['collegemid'];
				$losmid=$rt['levelofstudy'];
				$countrymid=$rt['countrymid'];
				$cnmid=$rt['cnmid'];
				
				$cmpbroucher=$rt['cmpbroucher'];
				$cmcoursebroucher=$rt['cmcoursebroucher'];
				
				$pidedit="<a href='courseedit.php?id=".$rt['coursemid']."' data-toggle='tooltip' data-placement='top' title='Edit' class='fa fa-edit fa-lg'>  </a>";
				
				$cmpbroucher1="<a target='_blank' href='./docs/$cmpbroucher'>download</a>";
				
				$cobroucher="<a target='_blank' href='./docs/$cmcoursebroucher'>download</a>";
				
				
				$piddelete="<a href='coursedelete.php?id=".$rt['coursemid']."' onclick='return confirm(&#39;Are you sure you want to delete ?&#39;);' data-toggle='tooltip' data-placement='top' title='Delete' class='far fa-trash-alt fa-lg'>  </a>";				
					$op=$pidedit."&nbsp;&nbsp;&nbsp; ".$piddelete;			
				$cmdesc=str_replace ("'","'",$rt['cmdesc']);
				$cmdesc=str_replace ('"','\"',$cmdesc);
				
				$cprogramlength=str_replace ("'","\'",$rt['cprogramlength']);
				$cprogramlength=str_replace ('"','\"',$cprogramlength);
				
				$ctutionfees=str_replace ("'","\'",$rt['ctutionfees']);
				$ctutionfees=str_replace ('"','\"',$ctutionfees);
				
				$cprogramtype=str_replace ("'","\'",$rt['cprogramtype']);
				$cprogramtype=str_replace ('"','\"',$cprogramtype);
				
				$cprogramcategory=str_replace ("'","\'",$rt['cprogramcategory']);
				$cprogramcategory=str_replace ('"','\"',$cprogramcategory);
				
				$cprogramlanguage=str_replace ("'","\'",$rt['cprogramlanguage']);
				$cprogramlanguage=str_replace ('"','\"',$cprogramlanguage);
				
				$cprogramdelivery=str_replace ("'","\'",$rt['cprogramdelivery']);
				$cprogramdelivery=str_replace ('"','\"',$cprogramdelivery);
				
				$costofliving=str_replace ("'","\'",$rt['costofliving']);
				$costofliving=str_replace ('"','\"',$costofliving);
				
				
					
						$cmname=$rt['cmname'];
						$cmpbroucher=$rt['cmpbroucher'];
						if($cmpbroucher==""){
							$cmpbroucher1="";
							}
						else{
						$cmpbroucher1="<a target='_blank' href='./docs/$cmpbroucher'>download</a>";
						}
						$cmcoursebroucher=$rt['cmcoursebroucher'];
						if($cmcoursebroucher==""){
							$cmcoursebroucher1="";
							}
						else{
						$cmcoursebroucher1="<a target='_blank' href='./docs/$cmcoursebroucher'>download</a>";
						}
						
						
					
					
					$countryname="";
					$eve5= "select * from   res_country_master where countrymid=$countrymid";
					$re5 = mysqli_query($conn, $eve5);
					 while($rt5 = mysqli_fetch_assoc($re5))
					{
			
						//$losmid=$rt4['losmid'];
						$countryname=$rt5['countryname'];
						
					}
					$coursename="";
					$eve55= "select * from   res_coursename_master where cnmid=$cnmid";
					$re55 = mysqli_query($conn, $eve55);
					 while($rt55 = mysqli_fetch_assoc($re55))
					{
			
						
						$coursename=$rt55['coursename'];
						
					}
					$levelofstudy="";
					$eve55= "select * from   res_levelofstudy_master where losmid=$losmid";
					$re55 = mysqli_query($conn, $eve55);
					 while($rt55 = mysqli_fetch_assoc($re55))
					{
			
						$losmid=$rt55['losmid'];
						$levelofstudy=$rt55['levelofstudy'];
						
					}
					
				
				
					
					
    $data[] = array(
    		"coursemid"=>$coursemid,
    		"coursemid1"=>$i++,
    		"op"=>$op,
    		"cnmid"=>$coursename,
    		"cmdesc"=>$cmdesc,
    		"collegemid"=>$cmname,
    		"levelofstudy"=>$levelofstudy,
    		
    		"cprogramlength"=>$cprogramlength,
    		"ctutionfees"=>$ctutionfees,
    		"cprogramcategory"=>$cprogramcategory,
    		"cprogramlanguage"=>$cprogramlanguage,
    		"cprogramtype"=>$cprogramtype,
    		"cprogramdelivery"=>$cprogramdelivery,
    		"costofliving"=>$costofliving,
    		"cmpbroucher"=>$cmpbroucher,
    		"cmcoursebroucher"=>$cmcoursebroucher1,
    		"countrymid"=>$countryname,
    		"cmpbroucher"=>$cmpbroucher1,
    		
    		
    		
    		
			

				
			
    	);
}

## Response
$response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);
