<?php
session_start();
//$menu = $_SESSION['menu'];
//include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:11pt; }
<p style="font-size:11px;"></p>
</style>



</head>
<body>
<h3> ΟΜΑΔΕΣ ΕΞΕΤΑΖΟΜΕΝΩΝ ΜΑΘΗΜΑΤΩΝ</h3>

<?php 

require("dbconnect.php");

$query = "SELECT mathima, COUNT(mitroo) as cm FROM exetb GROUP BY mathima order by cm DESC"; 
	 
$result = mysqli_query($con,$query) or die(mysqli_error());

// Print out result
$i=0;
while($row = mysqli_fetch_array($result)){
	$math[$i]=$row['mathima'];
	$plithos[$i]=$row['cm'];
		echo  $math[$i] ."  (".$plithos[$i]."  ) ";
	echo "<br />";
	$i++;
}
$c=$i;
echo "<br />";
echo "<br>"."ΣΥΝΟΛΟ ΜΑΘΗΜΑΤΩΝ: ".$c."<br>";

//create arrays for mathimata
for ($i=0;$i<=$c-1;$i++) 
{
$k=0;
$sql=mysqli_query($con,"SELECT mitroo,mathima FROM exetb");
while ($sq=mysqli_fetch_array($sql))
{
    $mitr=$sq['mitroo'];
	$l1= $sq['mathima']; 
	$les=$math[$i];
	if ($les == $l1 )
	{
	   $exet[$math[$i]][$k]=$mitr;
	   $k++;
	}
}
}

//print_r($exet);


$c=count($math);
$k=0;
$group= array();
while( $c > 1) {
  $j=1;
  $group[$j]=$math[0];
  $mathites=count($exet[$math[0]]);
  $ar1=$exet[$math[0]];

  $i=1;
  $c=count($math);
  while($i < $c) 
  {
    $ar2=$exet[$math[$i]];
	$res=array_diff($ar1,$ar2 );
    $c1=count($res);
    $c2=count($ar1);
	if ($c1==$c2) { 
       $group[$j]=$group[$j].",".$math[$i];
	   $mathites=$mathites+count($ar2);
	   $ar1 = array_merge($ar1, $ar2);
       }
   $i++;	
  }
echo "<br>";
print_r($group[$j]);
echo "<br>";echo "ΣΥΝΟΛΟ ΜΑΘΗΤΩΝ : ".$mathites;
$toremove = explode(',', $group[$j]);
foreach ($toremove as $remmath) {
   if (($key = array_search($remmath, $math)) !== false) {
	array_splice($math, $key, 1);
	}

}
$j++;

echo "<br>";
$mathites=0;
}


echo "<br>"."............................."."<br>";

?>

<br /><br />
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού  </a>
</body>
</html>