<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
require "dbconnect.php";
$dt=date('Y-m-d');
$sqlo=mysqli_query($con,"select code,omilos from omiloi");
$i=0;$om=array();$tm=$idm=$name=$mit=array();
while ($rowo = mysqli_fetch_array($sqlo))
{
	$i++;
	
	$om[$i]=$rowo['omilos'];
	$cod=$rowo['code'];
	$sql = mysqli_query($con,"SELECT d.*, al.mitroo,al.tmima,al.eponimo,al.onoma 
	FROM allstudentinfo as al,dilosiomilosstu as d WHERE d.mitroo=al.mitroo  and d.code='$cod' ORDER BY d.code ASC,al.tmima ASC" ); 
	$k=0;
	while ($row = mysqli_fetch_array($sql)) 
	{
		$k++;
		$id=$row['mitroo'];
		$idm[$k]=$row['mitroo'];
		$mit[$cod][$id]=$row['mitroo'];
		$tm[$cod][$id]=$row['tmima'];
		$name[$cod][$id]= $row['eponimo']." ".$row['onoma'];
		
	}

}
//..............................
$xls_filename = 'omiloi_'.date('Y-m-d').'.xls'; // Define Excel (.xls) file name

//create a file and send to browser for user to download
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$xls_filename");
header("Pragma: no-cache");
header("Expires: 0"); 
 
// Start of printing column names as names of MySQL fields
$header= "A/A". ","."ΜΗΤΡΩΟ". ","."ΤΜΗΜΑ". ","."ΜΑΘΗΤΗΣ/ΤΡΙΑ".","."ΑΠΟΥΣΙΑ";


// Start while loop to get data


	
	$k=1;
	foreach($mit as $mo)
	{
		$data=$om[$k];
		$data = trim( $data ) . "\n";
		$data = str_replace( "\r" , "" , $data );
		print "$data";
		print "$header\n";
		$j=1;
		foreach($mo as $mitr)
		{
		$data = $j.",".$mo[$mitr].",".$tm[$k][$mitr].",".$name[$k][$mitr];
		$data = trim( $data ) . "\n";
		$data = str_replace( "\r" , "" , $data );
		print "$data";
		$j++; 
		}
		$k++;
	}



?>
</body>
</html>