<?php
//---------------------------------------------------------	
$dayf=array(" ","Mon","Tue","Wed","Thu","Fri");
$sql1 = mysqli_query($con,"SELECT mitroo FROM allstudentinfo WHERE tmima='$tmimaid' " );
$k=1;
$mathx=array();
while ($rowall = mysqli_fetch_array($sql1))
{
  $mitr=$rowall['mitroo'];
  $sql = mysqli_query($con,"SELECT id FROM stutimetable WHERE mitroo='$mitr' " );
  $row = mysqli_fetch_array($sql);
  $idx=$row['id']+1;
   
  $sql = mysqli_query($con,"SELECT * FROM stutimetable WHERE id='$idx' " );
  $row = mysqli_fetch_array($sql);
  $row=array_slice($row, 2);
  
  for ($x=1;$x<=5;$x++){
  for ($per=1;$per<=$gym;$per++)
  {
	$dayper=$dayf[$x].$per;
	$m=$row["$dayper"];
	$ar=explode("/",$m);
	$mathx[$k]=$ar[1];
	$k++;
  }
  } 
  
}
$mathx=array_filter($mathx);
$mathall=array_unique($mathx);
//-------------	
?>
<h3> ΑΠΟΥΣΙΕΣ ΚΑΤΑ ΜΑΘΗΜΑ ΓΙΑ ΤΟ ΤΜΗΜΑ: <?php echo $tmimaid; ?> </h3>
<?php
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>A/A</th>
<th>ΜΗΤΡΩΟ</th>
<th>ΜΑΘΗΤΗΣ / ΤΡΙΑ</th>";
foreach($mathall as $les)
{
	echo "<th>".$les."</th>";
}
echo "</tr>";
$cols=count($mathall);
	
$sql1 = mysqli_query($con,"SELECT mitroo,onoma,eponimo FROM allstudentinfo WHERE tmima='$tmimaid' ORDER by eponimo,onoma " );
$k=1;
while ($rowall = mysqli_fetch_array($sql1))
{
  foreach($mathall as $les){$mathc[$les]=0;}
  $ap=0;
  $mitr=$rowall['mitroo'];
  echo "<td>".$k. "</td>"; 
  echo "<td>".$mitr."</td>";
  echo "<td>".$rowall['eponimo']." ".$rowall['onoma']."</td>"; 
  //******************************
  $sql = mysqli_query($con,"SELECT dateout, period, tetramino FROM apousies WHERE mitroo='$mitr' AND apousia=1  ORDER BY dateout,period " );
  $dayper=$tetr=$math=$mathc=array();
  $i=1;
  while($row = mysqli_fetch_array($sql)) 
  {    
	  $day = date('D', strtotime($row['dateout']));
	  $dayper[$i]=$day.$row['period']; 
	  $tetr=$row[tetramino];
	  if ($tetr == 'Α' ) {$db='stutimetablea';} else {$db='stutimetable';}
	  
	  $sqla = mysqli_query($con,"SELECT id FROM ".$db. " WHERE mitroo='$mitr' " );
	  $rowa = mysqli_fetch_array($sqla);
	  $idx=$rowa['id']+1;
	  $sqla = mysqli_query($con,"SELECT ".$dayper[$i]." FROM ".$db. " WHERE id='$idx' " );
	  $rowa = mysqli_fetch_array($sqla);
	  $m=$rowa["$dayper[$i]"];
	  $ar=explode("/",$m);
	  $math[$i]=$ar[1];
	  $i++;
  }
  $math=array_filter($math);
  $mathtm=array_unique($math);

  foreach ($math as $mt)
  {
	$mathc[$mt]++;
  }

  foreach ($mathall as $mt)
  {
    echo "<td>".$mathc[$mt]."</td>";
  }
  //**********************************
  echo "</tr>";
  
}
	 
echo "</table>";	