<?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.cmfacility like '%".$searchValue."%'  or  res_course_master.cmduration like '%".$searchValue."%'  or res_course_master.cmfees like '%".$searchValue."%'  or res_course_master.cmeligibility like '%".$searchValue."%'  or res_course_master.cmremarks like '%".$searchValue."%'  or res_course_master.cmother 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'];
		
		$placeholders = implode(',', $countrymid);

		if(in_array("All",$countrymid))
		{
		$aa="";
		}
		else
		{
			 $aa=" and res_course_master.countrymid in ($placeholders) ";
			 $aa1=" and countrymid in ($placeholders) ";
		}
	}
	
	$bb="";
	$bb1="";
	if( isset($_SESSION['cnmid']))
	{
		$cnmid = $_SESSION['cnmid'];
		$placeholders1 = implode(',',$cnmid);
		if(in_array("All",$cnmid))
		{
		
		}
		else
		{
				
			 $bb=" and res_course_master.cnmid in ($placeholders1)";
			 $bb1=" cnmid in ($placeholders1)";
		}
	}
	

   /* $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= "elect count(*) as allcount from res_course_master,res_coursename_master WHERE 1 $aa $bb".$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".$searchQuery );
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];


## Fetch records
  $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 ".$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);
				
				$cmduration=str_replace ("'","\'",$rt['cmduration']);
				$cmduration=str_replace ('"','\"',$cmduration);
				
				$cmfees=str_replace ("'","\'",$rt['cmfees']);
				$cmfees=str_replace ('"','\"',$cmfees);
				
				$cmdocument=str_replace ("'","\'",$rt['cmdocument']);
				$cmdocument=str_replace ('"','\"',$cmdocument);
				
				$cmeligibility=str_replace ("'","\'",$rt['cmeligibility']);
				$cmeligibility=str_replace ('"','\"',$cmeligibility);
				
				$cmfacility=str_replace ("'","\'",$rt['cmfacility']);
				$cmfacility=str_replace ('"','\"',$cmfacility);
				
				$cmremarks=str_replace ("'","\'",$rt['cmremarks']);
				$cmremarks=str_replace ('"','\"',$cmremarks);
				
				$cmother=str_replace ("'","\'",$rt['cmother']);
				$cmother=str_replace ('"','\"',$cmother);
				
				
					
						$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,
    		
    		"cmduration"=>$cmduration,
    		"cmfees"=>$cmfees,
    		"cmeligibility"=>$cmeligibility,
    		"cmfacility"=>$cmfacility,
    		"cmdocument"=>$cmdocument,
    		"cmremarks"=>$cmremarks,
    		"cmother"=>$cmother,
    		"cmpbroucher"=>$cmpbroucher,
    		"cmcoursebroucher"=>$cmcoursebroucher1,
    		"countrymid"=>$countryname,
    		"cmpbroucher"=>$cmpbroucher1,
    		
    		
    		
    		
			

				
			
    	);
}

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

echo json_encode($response);
