<?php
session_start(); // start up your PHP session!
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
require("dbconnect.php");
$am=$_POST['am'];
$sql = mysqli_query($con,"SELECT apousies.dateout, apousies.mitroo, apousies.period, allstudentinfo.tmima,allstudentinfo.eponimo,allstudentinfo.onoma FROM apousies,allstudentinfo WHERE apousia=1 AND apousies.mitroo=allstudentinfo.mitroo AND apousies.mitroo='$am' ORDER BY allstudentinfo.tmima,allstudentinfo.mitroo, apousies.dateout,apousies.period " );
// Print out result
echo "<table border='1'>
<tr>
<th>Ημερομηνία</th>
<th>AM</th>
<th>Τμήμα</th>
<th>Όνομα</th>
<th>Περ1</th>
<th>Περ2</th>
<th>Περ3</th>
<th>Περ4</th>
<th>Περ5</th>
<th>Περ6</th>
<th>Περ7</th>
</tr>";	
$row = mysqli_fetch_array($sql);
while(!empty($row)) {
	$j=1;
    while($j<=7){$per1[$j]="  ";$j++;}
	  $pmp=$row['mitroo'];
	  $dt=$row['dateout'];
	  $tmima=$row['tmima'];
	  $eponimo=$row['eponimo'];
	  $onoma=$row['onoma'];
	  echo "<td>". $dt . "</td>";
	  echo "<td>". $pmp . "</td>";
	  echo "<td>". $tmima . "</td>";
	  echo "<td>". $eponimo."  ".$onoma . "</td>";
	  while ($pmp==$row['mitroo']){
	  $per1[$row['period']]="XXXX";
	  $row = mysqli_fetch_array($sql);}
    

     $i=1;    
     while($i<=7){
		 echo "<td>". $per1[$i] . "</td>";$i++;}
     echo "</tr>";
   
}
echo "</table>";	
?> 
</body>
</html>