<?php
session_start(); 
include 'connect.php';
$selected_ids = $_POST['coursemid'];

// Connect to database and retrieve selected data
// ...

// Generate HTML table with only selected data
$html = '<table >';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>Course Name</th>';
$html .= '<th>Credentials</th>';
$html .= '<th>College</th>';
$html .= '<th>Country</th>';
$html .= '<th>Duration</th>';
$html .= '<th>Fees</th>';
$html .= '<th>Eligibility</th>';
$html .= '<th>Facility</th>';
$html .= '<th>Documents</th>';
$html .= '<th>Remarks</th>';
$html .= '<th>Other</th>';

$html .= '</tr>';
$html .= '</thead>';
$html .= '<tbody>';


foreach ($selected_ids as $coursemids) {
	
	
	$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 and res_course_master.coursemid = $coursemids";
//$empQuery = "select * from res_inquiry_master WHERE 1 ";
$empRecords = mysqli_query($conn, $empQuery);
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'];
				$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'];
						
					}
}
					
				
  $html .= '<tr>';
  $html .= '<td>' . $coursename . '</td>';
  $html .= '<td>' . $levelofstudy . '</td>';
  $html .= '<td>' . $cmname . '</td>';
  $html .= '<td>' . $countryname . '</td>';
  $html .= '<td>' . $cmduration . '</td>';
  $html .= '<td>' . $cmfees . '</td>';
  $html .= '<td>' . $cmeligibility . '</td>';
  $html .= '<td>' . $cmfacility . '</td>';
  $html .= '<td>' . $cmdocument . '</td>';
  $html .= '<td>' . $cmremarks . '</td>';
  $html .= '<td>' . $cmother . '</td>';
  $html .= '</tr>';
}

$html .= '</tbody>';
$html .= '</table>';

// Output HTML as a download file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="export.xls"');
header('Cache-Control: max-age=0');
echo $html;
?>