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

<body>
<?php
require "dbconnect.php";
$dt=$_SESSION['dt'];
$gym=$_SESSION['gym'];

	
$s=$dbcon->prepare("select mitroo from apallages ");
$s->execute();
$stuin=array();
$i=1;
while($row=$s->fetch(PDO::FETCH_ASSOC))
{
	$stuin[$i]=$row['pmp'];
	$i++;
}
mysqli_query($con,'TRUNCATE TABLE apallages');
$sql = "SELECT mitroo,tmima,eponimo,onoma  FROM allstudentinfo order by tmima ASC ";
$result = $dbcon->prepare($sql);
$result->execute();
?>
<form  method="post" name ="epilogi" >
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:520px; height: 650px">
<?php
echo "<table style='width:500px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>ΜΗΤΡΩΟ</th>
<th>ΤΜΗΜΑ</th>
<th>ΟΝΟΜΑΤΕΠΩΝΥΜΟ</th>
<th></th>
<th>ΕΠΙΛΟΓΗ</th>
</tr>";

$i=1;
while ($row=$result->fetch(PDO::FETCH_ASSOC)) 
{ 
    $am[$i]=$row['mitroo'];
	if(in_array($am[$i],$stuin))
	{
	   $checked = "checked";
	}
	else
	{
		$checked = "";
    }
	$tm1[$i]=$row['tmima'];
	$namet[$i]=$row['eponimo']." ".$row['onoma'];;
	echo "<tr>";
	echo "<td>".$am[$i]."</td>";
	echo "<td>".$tm1[$i]."</td>";
	echo "<td>".$namet[$i]."</td>";
	echo "<td>"." "."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='mitr[]' value='$am[$i]'  {$checked} >"."</td>";
	echo "</tr>";
    $i++;
}
echo "</table>";
?>
</div>
<br>
<input type="submit" name="apallages" value="Επιβεβαίωση Επιλογών" >
<?php

if (isset($_POST['apallages']))
{
$mitr = $_POST['mitr'];

foreach ($mitr as $mitroo)
{
   $sql3 = $dbcon->prepare("SELECT tmima FROM allstudentinfo where mitroo=? ");
   $sql3->execute([$mitroo]);
   $row3=$sql3->fetch(PDO::FETCH_ASSOC);
   $tmima=$row3['tmima']; 
   $sql3 = $dbcon->prepare("SELECT id FROM stutimetable where mitroo=? ");
   $sql3->execute([$mitroo]);
   $row3=$sql3->fetch(PDO::FETCH_ASSOC);
   $idx=$row3['id']+1; 
   $sql3 = $dbcon->prepare("SELECT * FROM stutimetable where id=? ");
   $sql3->execute([$idx]);
   $row3=$sql3->fetch(PDO::FETCH_ASSOC);
   $x2=array_slice($row3, 1);
   foreach ($x2 as $key => $value) 
   { 
	   $i=0;
       if (strpos($value, 'Θρησκευτικά') !== false)
       {
         $i++;
		 $dayout=substr($key,0,3);
		 $perout=substr($key,-1,1);
		 $ar=explode("/",$value);
		 $teachout=$ar[2];
		 $teachin='';
         $sql1="INSERT INTO apallages (mitroo,code,dayout,perout,teachout,teachin) 
				values ('$mitroo',' ','$dayout','$perout','$teachout','$teachin')" ;
				if(!mysqli_query($con, $sql1))
				{
					echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
				}
			
		}
   } 
  }
 echo "Ο ΠΙΝΑΚΑΣ ΔΗΜΙΟΥΡΓΗΘΗΚΕ ΜΕ ΕΠΙΤΥΧΙΑ";
}
 ?>
<br /><br />

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