<?php
include "scrinit.php";

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>";
//initialize array

$sql = mysqli_query ($con, "SELECT dateout, mitroo,period,apousia,kath,mathima,mathc,stoli FROM apousies 
WHERE teacher='$usr' AND dateout='$dt' AND stoli=0 ORDER BY period,apousia " );

$c3=mysqli_num_rows($sql);
$row=mysqli_fetch_array($sql);

$sqlsy=	$dbcon->prepare("SELECT per,perigrafi,mathima FROM syllabi WHERE pmp=? AND date=? ORDER BY per " );
$sqlsy->execute([$usr,$dt]);
$c2=$sqlsy->rowCount();

$lesson=$subject=$perigrafi=array();

while ($rowsy = $sqlsy->fetch(PDO::FETCH_ASSOC))
{
   		$perigrafi[$rowsy['per']]=$rowsy['perigrafi'];
}
//build apousiologio ...........................
$sql2 = $dbcon->prepare("SELECT tmima,eponimo,onoma FROM allstudentinfo WHERE mitroo=? " );
$per=1;$k=1;
while(!empty($row) ) 
{ 
      while ($per != $row['period'] AND !empty($row) )
	  {
		 echo "<tr  bgcolor='silver' >";
		 echo "<td>".$per."</td>";
	     echo "<td>"." " ."</td>";
		 echo "<td>"." ". "</td>";
	     echo "<td>"." ". "</td>";
	     echo "<td>"." ". "</td>";
	     echo "<td>"." ". "</td>";
		 echo "<td>"." ". "</td>";
	     echo "</tr>";
		 $per++;
	  }
	  echo "<td>".$row['period'] . "</td>";
	  $math=$row['mathima'];
	  $mathc=$row['mathc'];
	  echo "<td>". $row['mathima']."</td>";
	  $perio[$row['period']]=$row['period'];
	  $j=1;
	  while ($row['apousia']==1 AND $per==$row['period'])
	  {
	  $m=$row['mitroo'];
	  $sql2->execute([$m]);
      $row2=$sql2->fetch(PDO::FETCH_ASSOC);
	  	
	  if ($j <> 1 )
	  {
	    echo "<td>". " " . "</td>";
	    echo "<td>". " " . "</td>";
	  }
	  
	  echo "<td>".$row['mitroo']. "</td>";
	  echo "<td>". trim($row2['eponimo'])."  ".trim($row2['onoma']) . "</td>";
	  echo "<td>". $row2['tmima'] . "</td>";
	  echo "<td>". "  " . "</td>";
	  if ($j==1) {
	   echo "<td>"."<textarea name='syllabi[]'> $perigrafi[$per] </textarea >"."</td>";
	  } else
	  {echo "<td>". "  " . "</td>";}
	  echo "</tr>";
	  $j++;
	  $row=mysqli_fetch_array($sql);
	  }
	  
	   //kataxorisi kathisteriseon //////////////////////////////////////////////////////
	  $k=1;
	  while ($per==$row['period'])
	  {
	  $m=$row['mitroo']; 
	  $sql2->execute([$m]);
      $row2=$sql2->fetch(PDO::FETCH_ASSOC);
	
	  if ($k <>1)
	  {
	    echo "<td>". " " . "</td>";
	    echo "<td>". " " . "</td>";
	  }
	  echo "<td>".$row['mitroo']. "</td>";
	   echo "<td>". trim($row2['eponimo'])."  ".trim($row2['onoma']) . "</td>";
	  echo "<td>". $row2['tmima'] . "</td>";
	  echo "<td>". $row['kath'] . "</td>";
	  if ($k==1) {
	   echo "<td>"."<textarea name='syllabi[]' > $perigrafi[$per] </textarea >"."</td>";
	  } else
	  {echo "<td>". "  " . "</td>";}
	   echo "</tr>";
	   
	  $k++;
	  $row=mysqli_fetch_array($sql);
	  }

	  $per++;
}

 while ($per  <= $gym AND empty($row))
	  {
		 echo "<tr  bgcolor='silver' >";
		 echo "<td>".$per. "</td>";
	     echo "<td>"." " .  "</td>";
		 echo "<td>"." ". "</td>";
	     echo "<td>". "  ". "</td>";
	     echo "<td>". " " . "</td>";
	     echo "<td>". "  " . "</td>";
		 echo "<td>"." "."</td>";
	     echo "</tr>";
		 $per++;
	  }	 
echo "</table>";	
 