<?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");

echo "<BR>"."<b>"."EΠΕΛΕΞΕ ΔΡΑΣΤΗΡΙΟΤΗΤΑ:"."</b>";
$sql = "SELECT onoma,code,date FROM ekdilosi WHERE katddk<>0 order by date desc";
$result = $dbcon->prepare($sql);
$result->execute();
?>
<form  method="post"  name="ekdilosi" > 
<br>
<?php
echo "<select name='ekd'>";
while ($row =$result->fetch(PDO::FETCH_ASSOC)) 
{
	$ekd=$row['onoma'];
	$code=$row['code'];
	echo "<option value ='".$code."'>".$ekd." (".$code.") "."</option>";
}
echo "</select>";
echo "<br>";
echo "<br >"."ΚΕΙΜΕΝΟ :"."<br >";; 
echo "<textarea name='keimeno' cols='80' rows='1'> $keimeno </textarea >"."<br >";
?>
<br>
<input type="Submit" name="ekd1" value="Επιλογή Δραστηριότητας" >

</form>	

<?php

if (isset($_POST['ekd1'])) 
{
   $code=$_POST['ekd'];

   $skopos=$_POST['keimeno'];
   $sql = $dbcon->prepare("SELECT a.tmima,a.onoma,a.eponimo FROM ekdilosistu as e,allstudentinfo as a WHERE e.pmp=a.mitroo and e.code=?");
   $sql->execute([$code]);
 
$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= "a/a". ","."tmima". ","."fylo1". ","."onoma". ","."skopos".","."fylo2";
print "$header\n";  

$i=1;
$ar=["Α","Η","Ν","α","η","ν"];
$ar1=["Σ","ς"];
$ar2=["Κ","Π","Τ","Ξ","Ψ","Α","Ε","Η","Υ","Ω","Ι"];
while ($row =$sql->fetch(PDO::FETCH_ASSOC)) 
{
	$am=$row['mitroo'];
	$tm1=$row['tmima'];
	$epo=$row['eponimo'];
	$nam = $row['onoma'];
	//$epo = mb_strtoupper($epo, "UTF-8");
	//$epo1 = mb_convert_case($epo, MB_CASE_UPPER, "UTF-8");
	$f="στο";
	$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 (in_array($last, $ar)) {$f="στη";$fx="της";}
	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)) {$f=$f."v";}
	$name=$epo." ".$nam;

	$data = $i.",".$tm1.",".$f.",".$name.",".$skopos.",".$fx;

	$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>