<?php
//---------------------------------------------------------	
$dayf=array(" ","Mon","Tue","Wed","Thu","Fri");
$sql1 = mysqli_query($con,"SELECT mitroo FROM allstudentinfo WHERE tmima='$tmimaid' " );
$k=1;
$mathx=$eptam=$mathpers=$outpout=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);
  $l=1;
  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];
	$mathpers[$l]=$ar[1];
	$l++;
	$k++;
  }
  }
  //$eptam=array_count_values($mathx);
  $output = array_merge(array_diff_key($eptam, $mathpers), array_diff($mathpers, $eptam));
  $output = array_diff($mathpers,$mathx);
  $mathpers=array();
}
$mathx=array_filter($mathx);
echo "<br>";

$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 a.dateout, a.period, a.tetramino, a.mitroo FROM apousies as a WHERE a.mitroo='$mitr' AND a.apousia=1 AND (a.mitroo not in (select mitroo from apousies where kath > 0 and period=a.period)) ORDER BY dateout,period " );
  $dayp=$math=array();
  $i=1;
  while($row = mysqli_fetch_array($sql)) 
  {    
	  $day = date('D', strtotime($row['dateout']));
	  $p=$row['period'];
	  if($p <= $gym)
	  {
	  $dayp[$i]=$day.$p;
	  $tetr=$row['tetramino'];
	  //if ($tetr == 'Α' ) {$db='stutimetable';} else {$db='stutimetable';}
	  $db='stutimetable';
	  $sqla = mysqli_query($con,"SELECT id FROM ".$db. " WHERE mitroo='$mitr' " );
	  $rowa = mysqli_fetch_array($sqla);
	  $idx=$rowa['id']+1;
	  $xx=$dayp[$i];
	  $sqla = mysqli_query($con,"SELECT ".$xx." FROM ".$db. " WHERE id='$idx' " );
	  $rowa = mysqli_fetch_array($sqla);
	  $m=$rowa["$xx"];
	  $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>";
  $k++;
}
	 
echo "</table>";	