<?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');
   
$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 " ); 
//..............................

$xls_filename = 'export_'.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". ","."mitroo". ","."tmima". ","."onoma". ","."omilos1".","."omilos2";
print "$header\n";
 
// Start while loop to get data
$i=1;
while ($row = mysqli_fetch_array($sql)) 
{
$id[$i]=$row['mitroo'];
$tmima=$row['tmima'];
$omilos=$row['code'];
$name= $row['eponimo']." ".$row['onoma'];

$data = $i.",".$id[$i].",".$tmima.",".$name.",".$omilos;
$data = trim( $data ) . "\n";
$data = str_replace( "\r" , "" , $data );
print "$data";
  $i++; 
}

//..............................
$sql = mysqli_query($con,"SELECT mitroo FROM allstudentinfo " ); 
$i=1;
while ($row = mysqli_fetch_array($sql)) 
{ 
$mitr[$i]=$row['mitroo'];
$i++; 
}

$celement = array_diff($mitr,$id);
$ll=count($celement);	 
echo $ll."<br>";
//print_r ($celement);
$i=1;
foreach($celement as $id) 
{
   $sql = mysqli_query($con,"SELECT tmima,eponimo,onoma FROM allstudentinfo WHERE mitroo='$id' " ); 
   $row = mysqli_fetch_array($sql);
   $name=$row['eponimo']." ".$row['onoma'];
   $tmima=$row['tmima'];
   if ($i <15 ){
   $data = $i.",".$id.",".$tmima.",".$name.","."15";}
   else{ 
    $data = $i.",".$id.",".$tmima.",".$name.","."16";}
   $data = trim( $data ) . "\n";
   $data = str_replace( "\r" , "" , $data );
    print "$data";
   $i++;
}

?>
</body>
</html>