<?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 = $dbcon->prepare("SELECT dateout, mitroo,period,apousia,kath,mathima,mathc FROM apousies 
WHERE( teacher=? AND dateout=? and stoli <> 1)  ORDER BY period,apousia " );
$sql->execute([$usr,$dt]);
$c3=$sql->rowCount();
$row=$sql->fetch(PDO::FETCH_ASSOC);

$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();
$q = $dbcon->prepare("SELECT * FROM dailytab WHERE pmp =?");
$q->execute([$usr]);
$rowx=$q->fetch(PDO::FETCH_ASSOC);
$id=$rowx['id'];
$i=1;
$gym1=$gym;
while ($i<=$gym1) 
{
      $in='day'.$i;
      $lesson[$i]= $rowx["$in"];
      $i++;
}
$id=$id+1;
$q = $dbcon->prepare("SELECT * FROM dailytab WHERE id =?");
$q->execute([$id]);
$rowx=$q->fetch(PDO::FETCH_ASSOC);
$i=1;
while ($i<=$gym1) {
      $in='day'.$i;
      $d1=$rowx["$in"];
	  $ar=explode("/",$d1);
      $subject[$i]= $ar[1];
      $i++;
}

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']."/".$subject[$row['period']]."</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=$sql->fetch(PDO::FETCH_ASSOC);
	  }
	  
	   //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=$sql->fetch(PDO::FETCH_ASSOC);
	  }

	  $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>";	
 