<?php
$pmpx="";   
$less="";
$room="";
$found=0;
$resultp = mysqli_query($con,"SELECT c.synd,c.pmp FROM syndkath as c,syndidaskalies as s,syndcodes as d 
WHERE s.syndid=d.syndid and c.synd=d.synd and s.mitroo='$mitroo'");
$c1=mysqli_num_rows($resultp);
if ($c1 > 0 ) 
{
	while ($row4 = mysqli_fetch_array($resultp) AND $found==0) 
	{
	  $pmpx=$row4['pmp'];
	  $less=$row4['synd'];
	  $sqly = mysqli_query($con,"SELECT id,".$day1." FROM timetable WHERE pmp='$pmpx' AND ".$day1."='$less'") ;
	  $c=mysqli_num_rows($sqly);
	  if ($c > 0) 
	  {
		   $rowy = mysqli_fetch_array($sqly);
		   $id=$rowy['id'];
		   $nid=$id+1;
		   $res1 = mysqli_query($con,"SELECT ($day1) FROM timetable WHERE id='$nid'  " ); 
		   $row3 = mysqli_fetch_array($res1); 
		   $room= $row3["$day1"];
		   $found=1;
	  } 
	}
}
     
if ($found==0)
	 {

	 $resultx = mysqli_query($con,"SELECT id,pmp,Teacher,($day1)  FROM timetable WHERE ($day1)='$tmima' ");
	 $c=mysqli_num_rows($resultx);
	 $rowx = mysqli_fetch_array($resultx);
	 $name=$rowx['Teacher'];
	 $less=$rowx["$day1"];
	 $id=$rowx['id'];
	 $nid=$id+1;
	 $teach=$name;
	 $pmpx=$rowx['pmp'];
	 $res1 = mysqli_query($con,"SELECT * FROM timetable WHERE id='$nid'  " ); 
     $row3 = mysqli_fetch_array($res1); 
	 $room= $row3["$day1"]; 
}
	 
