<?php 
include "scrinit.php";
include $menu;
?>
<!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" />
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:1px;font-size:10pt; }
<p style="font-size:10px;"></p>
</style>
<title>SMS Πρώτης Περιόδου</title>

<?php 

require("dbconnect.php");
$usr=$_SESSION['usr'];
$access=$_SESSION['access'];
$dt=$_SESSION['dt'];
$gym=$_SESSION['gym'];

$sql = $dbcon->prepare("SELECT a.dateout,a.mitroo,a.period,al.kinito,al.tmima,al.eponimo,al.onoma FROM apousies as a,allstudentinfo as al 
WHERE a.apousia=1 AND a.dateout=? AND a.mitroo=al.mitroo ORDER BY al.tmima,a.mitroo,a.period " );
$sql->execute([$dt]);
$c=$sql->rowCount();
$row=$sql->fetch(PDO::FETCH_ASSOC);
$pr=1;

if ($c > 0)  
{  

$xls_filename = 'apousiesimeras_'.date('Y-m-d').'.txt'; // 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".","."Mitroo".","."Tmima".","."Onoma".","."per1".","."per2".","."per3".","."per4".","."Per5".","."per6".","."per7".","."thlefono".","."Ekdilosi";
print "$header\n";

$cc=0;
while(!empty($row)) 
{
	$per=$pera=array();
	$mt=$row['mitroo'];
	$tmima=$row['tmima'];
	$name=$row['eponimo']." ".$row['onoma'];
	$kinito=trim($row['kinito']);
	while ($mt==$row['mitroo'])
	  {
	     $per[$row['period']]="X";
	     $row=$sql->fetch(PDO::FETCH_ASSOC);
      }
	
	//.....elexos gia ekdiloseis tin parousa hmerominia
    $q=$dbcon->prepare("SELECT e.onoma,s.* FROM ekdilosi as e,ekdilosistu as s 
	                    WHERE e.date = ? and e.code=s.code and s.pmp=?");
    $q->execute([$dt,$mt]);
    $c1=$q->rowCount();
	if ($c1 > 0 ) 
	{
      $row2=$q->fetch(PDO::FETCH_ASSOC);
	  $onomaekd=$row2['onoma'];  
	}
	else
	{
	  $onomaekd="";	
	}
	
	for ($i=1;$i<=$gym;$i++)
	{
			if($per[$i]<>"")
			{
				$pera[$i]="X";
			}
			else
			{
				$pera[$i]=" ";	
			}
			if ($c1 > 0 ) 
			{
			  $p="p".$i;  
			  if ($row2["$p"]==1) {$pera[$i]="*X*"; }
			}			
	}
	
	$data = $cc.",".$mt.",".$tmima.",".$name.",".$pera[1].",".$pera[2].",".$pera[3].",".$pera[4].",".$pera[5].",".$pera[6].",".$pera[7].",".$kinito.",".$onomaekd;
    $data = trim( $data ) . "\n";
    $data = str_replace( "\r" , "" , $data );
    print "$data";
    $cc++;
}

}

?>
<br /><br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού  </a>
</body>
</html>