<?php
$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='$name')");
   $result = mysqli_query($con,"SELECT * FROM t1 " );
   $rowsub = mysqli_fetch_array($result);
   $id=$rowsub['id'];
   $id1=$id+1;
   mysqli_query($con,"INSERT INTO t1
   (SELECT id,Teacher,".$d1.",".$d2.",".$d3.",".$d4.",".$d5.",".$d6.",".$d7." FROM timetable WHERE id='$id1')"); 
  
  
  $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'  " );
       }
   }
   //$nper=$toper;
   //$day1=$day.$toper;
   $res = mysqli_query($con,"SELECT id,($day1) FROM t1 WHERE Teacher='$name' AND ($day1)='$less' " ); 
   //$cx=mysqli_num_rows($res);
   //if ($cx > 0) {
		       $row2 = mysqli_fetch_array($res);
			   $less=$row2["$day1"];
			   $id=$row2["id"]; $nid=$id+1;
	           $res1 = mysqli_query($con,"SELECT * FROM t1 WHERE id='$nid'  " ); 
               $row3 = mysqli_fetch_array($res1); 
	           $room= $row3["$day1"];
			   $found=1;
			   
	//}
   
   $result = mysqli_query($con,"SELECT id,Teacher,".$d1.",".$d2.",".$d3.",".$d4.",".$d5.",".$d6.",".$d7." FROM t1 WHERE id='$id'");
   $rowsub = mysqli_fetch_array($result);
   $i=1;
   while ($i<=7) {
     $in="$d".$i;
     $l=$rowsub["$in"];
     echo $i.$l." ";
     $i++;
   }
   echo "<br>";