<?php
//session_start();
//$day=$_SESSION['day'];
$lesson=array();

   $d1=$day."1";$d2=$day."2";$d3=$day."3";$d4=$day."4";$d5=$day."5";$d6=$day."6";$d7=$day."7";
   mysqli_set_charset($con, "utf8");
   mysqli_query($con,"DROP TEMPORARY TABLE IF EXISTS t1") ;
   $result = mysqli_query($con,"CREATE  TEMPORARY TABLE t1  (SELECT id,Teacher, ".$d1.",".$d2.",".$d3.",".$d4.",".$d5.",".$d6.",".$d7." FROM timetable WHERE Teacher='$teach')");
   $result = mysqli_query($con,"SELECT * FROM t1 " );
   $rowsub = mysqli_fetch_array($result);
   $id=$rowsub['id']+1;
   $result = mysqli_query($con,"INSERT INTO t1  (SELECT id,Teacher, ".$d1.",".$d2.",".$d3.",".$d4.",".$d5.",".$d6.",".$d7." FROM timetable WHERE id='$id')"); 
	
   $result = mysqli_query($con,"SELECT * FROM t1 " ); 
   $rowsub = mysqli_fetch_array($result);
   $n=mysqli_num_rows($result);
   $id=$rowsub['id'];
   $i=1;
   while ($i<=7) {
     $in=$day.$i;
     $l=$rowsub["$in"];
     $lesson[$i]= rtrim(ltrim($l));
     $i++;
   }

   $res = mysqli_query($con,"SELECT * FROM allages WHERE date='$dt' and idteacher= '$id' " );
   if (mysqli_num_rows($res) > 0)
   {
	   while ($rowu = mysqli_fetch_array($res)) 
	   {   $lf="";$ff="";$lt="";$lf="";
		   $id=$rowu['idteacher'];
		   $fromper=$rowu['fromper'];
		   $toper=$rowu['toper'];
		   $ff=$day.$fromper;$ft=$day.$toper;
		   $result = mysqli_query($con,"SELECT * FROM t1 WHERE id='$id'  " ); 
           $rowsub = mysqli_fetch_array($result);
           $lf=$rowsub["$ff"];
		   $lt=$rowsub["$ft"]; 
		   //echo $name.$lf."x".$lt."x".$ff."x".$ft;
		   mysqli_query($con,"UPDATE t1 SET ".$ft." = '$lf',".$ff."='$lt'  WHERE id='$id'  " );
	       $id1=$id+1;
		   $result = mysqli_query($con,"SELECT * FROM t1 WHERE id='$id1'  " ); 
           $rowsub = mysqli_fetch_array($result);
           $lf=$rowsub["$ff"];
		   $lt=$rowsub["$ft"];
           mysqli_query($con,"UPDATE t1 SET ".$ft." = '$lf',".$ff."='$lt'  WHERE id='$id1'  " );
       }
   }
   
   $ktse=$teacherse=$teacherapo=$roomam=$ktapo=$amper=array();
   $result1 = mysqli_query($con,"SELECT idtse,idtapo,period,room,id FROM amivallages WHERE date='$dt' ORDER BY id  ");
   $i=1;
   $day = date('D', strtotime($dt));
   while($rowu = mysqli_fetch_array($result1))
   {
	 $ktse[$i]=$rowu['idtse'];
	 $ktapo[$i]=$rowu['idtapo'];
	 $amper[$i]=$rowu['period'];
	 $roomam[$i]=$rowu['room'];
	 $dayper=$day.$amper[$i];
	 $sqlb=mysqli_query($con,"SELECT ".$dayper." FROM timetable WHERE id='$ktapo[$i]'");
     $rowb=mysqli_fetch_array($sqlb);
	 $les=$rowb["$dayper"];
	 mysqli_query($con,"UPDATE t1 SET ".$dayper." = '$les'  WHERE id='$ktse[$i]'  " );
	 mysqli_query($con,"UPDATE t1 SET ".$dayper." = ''  WHERE id='$ktapo[$i]'  " );
	 $i++;
   }	

 //........................
   
   $result = mysqli_query($con,"SELECT * FROM t1 WHERE Teacher='$teach'  " ); 
   $rowsub = mysqli_fetch_array($result);
   $id=$rowsub['id'];
   $i=1;
   while ($i<=7) {
     $in=$day.$i;
     $l=$rowsub["$in"];
     $lesson[$i]= rtrim(ltrim($l));
     $i++;
   }

?>

