<?php
include "scrinit.php";
include $menu;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<title>Πιστοποιητικά</title>
</head>

<body>
<h3> ΠΡΟΕΤΟΙΜΑΣΙΑ ΠΙΣΤΟΠΟΙΗΤΙΚΩΝ ΔΔΚ</h3>
<?php
require("dbconnect.php");
$sql = $dbcon->prepare("SELECT mitroo,tmima,onoma,eponimo,sex FROM allstudentinfo ORDER BY tmima ");
$sql->execute();
$count=$sql->rowCount();
$sql1 = $dbcon->prepare("SELECT * FROM ddk where mitroo=?");
$xls_filename = 'export_'.date('Y-m-d').'.csv'; // 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= "Α/Α". ","."ΤΜΗΜΑ". ","."ΦΥΛΟ1". ","."ΟΝΟΜΑ".","."ΦΥΛΟ2".","."ΦΥΛΟ3".","."ΒΑΘΜΟΣ".","."ΕΠΙΔΟΣΗ";
print "$header\n";  

while ($row=$sql->fetch(PDO::FETCH_ASSOC))
{
	
	$mitroo=$row['mitroo'];
	$sql1->execute([$mitroo]);
	$row1=$sql1->fetch(PDO::FETCH_ASSOC);
	$n=$sql1->columnCount() - 1;
	$sum=0; 
	for ($i=1;$i<=$n;$i++)
	{
	    $field="f".$i;
		$val=$row1["$field"];
	    $sum=$sum+$val ;	
	}
	$nam=$row['onoma'];
	$epo=$row['eponimo'];
	$tm1=$row['tmima'];
	$sex=$row['sex'];

$i=1;
$ar=["Α","Η","Ν","α","η","ν"];
$ar1=["Σ","ς"];
$ar2=["Κ","Π","Τ","Ξ","Ψ","Α","Ε","Η","Υ","Ω","Ι"];

	//$epo = mb_strtoupper($epo, "UTF-8");
	//$epo1 = mb_convert_case($epo, MB_CASE_UPPER, "UTF-8");
	$f="στον";
	$f1="Ο μαθητής";
	$fx="του";
	$epo= trim($epo);
	$nam= trim($nam);
	$l= mb_strlen($nam, "UTF-8") - 1;
	$l1=mb_strlen($epo, "UTF-8") - 1;
	$last=mb_substr($nam,$l,1,"UTF-8");
	$last1=mb_substr($epo,$l1,1,"UTF-8");
	if ($sex=='Θ') {$f="στη";$fx="της";$f1="Η μαθήτρια";}
	/*if (in_array($last1, $ar1)) 
	{
		$epo=mb_substr($epo,0,$l1,"UTF-8");
	}
	if (in_array($last, $ar1)) 
	{
		$nam=mb_substr($nam,0,$l,"UTF-8");
	}
	$first=mb_substr($epo,0,1,"UTF-8");*/
	if (in_array($first, $ar2) and $sex=='Θ') {$f=$f."v";}
	$name=$epo." ".$nam;
	$xr="";
	if ($sum > 19 ) {$xr="ΕΞΑΙΡΕΤΙΚΗ";}
	elseif ($sum >= 14) {$xr="ΠΑΡΑ ΠΟΛΥ ΚΑΛΗ";}
	elseif ($sum >=10 ) {$xr="ΠΟΛΥ ΚΑΛΗ";}
	elseif ($sum >=7 ) {$xr="ΚΑΛΗ";}
	elseif ($sum >=4 ) {$xr="ΙΚΑΝΟΠΟΙΗΤΙΚΗ";}
	else {$xr="ΜΕΤΡΙΑ";}
	
	$data = $i.",".$tm1.",".$f.",".$name.",".$fx.",".$f1.",".$sum.",".$xr;

	$data = trim( $data ) . "\n";
	$data = str_replace( "\r" , "" , $data );
	print "$data";
	$i++;
}

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