<?php
session_start(); // start up your PHP session! 
$menu=$_SESSION['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";
echo "<BR>"."ΑΠΟΤΕΛΕΣΜΑ ΕΡΕΥΝΑΣ"."<BR>";
$p=$_GET['per'];
$per=substr($p,0,1);
$am=substr($p,1);

    $sql = mysqli_query($con,"SELECT tmima FROM allstudentinfo WHERE mitroo='$am'  " ); 
    $row = mysqli_fetch_array($sql); 
	$tm1= $row["tmima"];    
	$dt=$_SESSION['dt'];
	$day = date('D', strtotime($dt)).$per;
	$lesson="";
	$tm2=str_replace("0"," ",$tm1);
	$tm3=str_replace("0","",$tm1);
	$sql = "SELECT mitroo,eponimo,onoma  FROM allstudentinfo WHERE mitroo='$am'";
    $result = mysqli_query($con,$sql);
	$row = mysqli_fetch_array($result);
	$nm=$row['eponimo']." ".$row['onoma'];
	
	$sql = "SELECT id,Teacher,($day)  FROM timetable WHERE ($day)='$tm1' OR ($day)='$tm2' OR ($day) like '%$tm3%' ";
	
	$result = mysqli_query($con,$sql);
	$c=mysqli_num_rows($result);
	if ($c > 0 ) {
		$row = mysqli_fetch_array($result);
		$name=$row['Teacher'];$id=$row['id'];
		$nid=$id+1;
	    $res1 = mysqli_query($con,"SELECT * FROM timetable WHERE id='$nid'  " ); 
        $row3 = mysqli_fetch_array($res1); 
	    $room= $row3["$day"];
	   
		}
	else
	{
	$sql = "SELECT sc.synd,sc.kathigitis  FROM syndcodes as sc,syndidaskalies as s 
	WHERE s.mitroo='$am' and s.syndid=sc.syndid";
    $result = mysqli_query($con,$sql);
	
   while ($row = mysqli_fetch_array($result)) 
   {
	$name=$row['kathigitis'];
	$lesson=$row['synd'];
   
	$res = mysqli_query($con,"SELECT * FROM timetable WHERE Teacher='$name' " ); 
    $row2 = mysqli_fetch_array($res);
    $id=$row2["id"];
	$lesson=$row2["$day"];
	$lesson=rtrim(ltrim($lesson));
	if (strlen($lesson)==4) {
	  $lesson=str_replace(" ","0",$lesson);}
	  	
	  if (($row['synd']==$lesson) OR ($lesson==$tm1))
	  {
        $nid=$id+1;
	    $res1 = mysqli_query($con,"SELECT * FROM timetable WHERE id='$nid'  " ); 
        $row3 = mysqli_fetch_array($res1); 
	    $room= $row3["$day"];
	   
	    break ;
	  }
	}
	}
	 

echo "<table class='myTable' >
<tr style='border:1px solid black' >
<th>Περίοδος</th>
<th>Τμήμα/Συνδιδασκαλια</th>
<th>Α.Μ</th>
<th>Όνοματεπώνυμο</th>
<th>Τμήμα</th>
<th>Αίθουσα</th>
<th> Καθηγητής / Ύλη</th>
</tr>";

		 echo "<tr >";
		 echo "<td>".$per. "</td>";
	     echo "<td>". $lesson.  "</td>";
		 echo "<td>".$am. "</td>";
	     echo "<td>". $nm. "</td>";
	     echo "<td>". $tm1. "</td>";
	     echo "<td>". $room. "</td>";
		 echo "<td>". $name . "</td>";
	     echo "</tr>"; 
echo "</table>";	 
	
 ?>

</body>
</html>