<?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'];
mysqli_query($con,'TRUNCATE TABLE stiriksi');	
$sql = $dbcon->prepare("SELECT * FROM timetable where mod(id,2)=1");
$sql->execute();
$i=1;
$st=$days=$pers=$pmpt=array();
while ($row=$sql->fetch(PDO::FETCH_ASSOC))
{
   $x2=array_slice($row, 3);
   foreach ($x2 as $key => $value) 
   {
	   //echo mb_substr($value, 0, 2,"utf-8");
	   
       if ((mb_substr($value, 0, 2,"utf-8") == "Στ") or (ltrim(rtrim($value))=="Αλλόγλωσσοι") )
	   {
        $st[$i]=ltrim(rtrim($value));
		$pmpt[$i]=$row['pmp'];
		$days[$i]=substr($key,0,3);
		$pers[$i]=substr($key,-1,1);
		$sql1 = mysqli_query($con,"SELECT s.mitroo FROM syndidaskalies as s,syndcodes as c
				where ltrim(rtrim(c.synd))='$value' and c.syndid=s.syndid ");
		$c=mysqli_num_rows($sql1);
		if ($c > 0)
		{
			while ($row1=mysqli_fetch_array($sql1))
			{
				$mitroo=$row1['mitroo'];
				$sql3 = $dbcon->prepare("SELECT tmima FROM allstudentinfo where mitroo=? ");
                $sql3->execute([$mitroo]);
				$row3=$sql3->fetch(PDO::FETCH_ASSOC);
				$tmima=$row3['tmima'];
				$day1=$key;
				$found=0;
				$resultp = mysqli_query($con,"SELECT c.synd,c.pmp  FROM syndidaskalies as s,syndcodes as c
					WHERE s.mitroo='$mitroo' and s.syndid=c.syndid and c.synd <> '$value' ");
                $c1=mysqli_num_rows($resultp);
                if ($c1 > 0 ) 
                {
                  while ($row4 = mysqli_fetch_array($resultp) AND $found==0) 
                  {
		           $pmp=$row4['pmp'];
	               $less1=trim($row4['synd']);
		           $sqly = mysqli_query($con,"SELECT id,".$day1." FROM timetable WHERE pmp='$pmp' AND trim(".$day1.")='$less1'") ;
	               $c=mysqli_num_rows($sqly);
		           if ($c > 0) 
		           {
		               $rowy = mysqli_fetch_array($sqly);
		               $less=$rowy["$day1"];
					   $found=1;
			       } 
                  }
                }
				if ($found==1)
	            {
				   $lout=$less;	
                }
				else
				{
					$lout=$tmima;	
				}
				 
				$sql2="INSERT INTO stiriksi (mitroo,lessonout,day,period,lessonin) 
				values ('$mitroo','$lout','$days[$i]','$pers[$i]','$value')" ;
				if(!mysqli_query($con, $sql2))
				{
					echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
				}
			}
		}
       } 
   }
   $i++;
 }
 echo "Ο ΠΙΝΑΚΑΣ ΔΗΜΙΟΥΡΓΗΘΗΚΕ ΜΕ ΕΠΙΤΥΧΙΑ";
 ?>
<br /><br />

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