<?php
session_start();include "index.php";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body>
<?php 
require("dbconnect.php");
if (isset($_POST['Submitapou'])) 
{

$usr=$_SESSION['pmp']; 
$per=$_SESSION['per'];
$less=$_SESSION['less'];
$perigrafi=$_POST['perigrafi'];
$teachapon=$_SESSION['kathigitis'];
	$c=0;$r=0;
    $dt = $_SESSION['dt'];;
	$dt = mysqli_real_escape_string($con, $dt);
	$mti = $_POST['mt'];
	$apousia = $_POST['att'];
	$c = count($apousia);
	 
    $_SESSION['c']=$c;
	
	
$sql=mysqli_query($con,"SELECT mitroo, apousia,kath,dateout,period,teacher,teachapon,mathima,stoli FROM apousies WHERE dateout='$dt' AND teacher='$usr' AND mathima='$less' AND period='$per' AND teachapon='$teachapon'" );
$c2=mysqli_num_rows($sql);

if ($c2 > 0) {
mysqli_query($con,"DELETE FROM apousies WHERE dateout='$dt' AND teacher='$usr' AND mathima='$less' AND period='$per' ");	
}

    $apc=0;
    for ($i=0; $i<$c; $i++) 
	{  
	    $apc=$apc+1;
		$sql="INSERT INTO apousies (mitroo, apousia,dateout,period,teacher,teachapon,mathima) VALUES ($apousia[$i],1,'$dt','$per','$usr','$teachapon','$less' ) ";

        if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         }  
         //.............................................. kataggelia
		
		 $sql = mysqli_query($con,"SELECT tmima FROM allstudentinfo WHERE mitroo=$apousia[$i]  " );
         $row = mysqli_fetch_array($sql);
         $tmimaid=$row['tmima'];
		 $sql=mysqli_query($con,"SELECT voi_id  FROM voithos  WHERE  class_id='$tmimaid' " );
         $row = mysqli_fetch_array($sql);
         $vdid = $row['voi_id'];
         $perigr = " ΑΠΟΥΣΙΑΖΕ ΑΔΙΚΑΙΟΛΟΓΗΤΑ ΤΗΝ ΩΡΑ ΤΗΣ ΑΝΑΠΛΗΡΩΣΗΣ ΤΗΝ ".$per." ΠΕΡΙΟΔΟ";
		 
		 
		 $sql=mysqli_query($con,"INSERT INTO kataggelia (aranaforas,mitroo,tmima,bd,kataggelon,datekata,perigrafi) VALUES (NULL,$apousia[$i],'$tmimaid','$vdid','$usr','$dt','$perigr') ");

	 
		 //..........................
     }

    $c1 = count($kathi);
	$r = 0;
	  
	for ($i=0; $i<$c1; $i++) 
	{    
	if ($kathi[$i] > 0 )
	{
        $sql="INSERT INTO apousies (mitroo,apousia,kath,dateout,period,teacher,teachapon,mathima) VALUES ('$mti[$i]',0,'$kathi[$i]','$dt','$per','$usr','$teachapon','$less') ";
	

        if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         }
		
		$r++;
	}}
	
	$cs = count($stoli);
	$s = 0;
	  
		for ($i=0; $i<$cs; $i++) {
	
       $sql="INSERT INTO apousies (mitroo, dateout,period,teacher,teachapon,mathima,stoli) VALUES ($stoli[$i],'$dt','$per','$usr',,'$teachapon','$less',1 ) ";

        if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         }
		
		$s++;}
	
}

 $all=$c+$r+$s; 
 echo "Προστέθηκαν ".$all."  εγγραφές"."<BR>";
$sql="INSERT INTO syllabi (pmp,per,date,perigrafi,mathima) VALUES ('$usr','$per','$dt','$perigrafi','$less') "; 
 if (!mysqli_query($con,$sql))
        {
         die('Error: ' . mysqli_error($con));
         }
//......................................................
if ($apc == 0) {                   //OLOI PARONTES



 $sql="INSERT INTO apousies (mitroo, dateout, period, teacher, mathima) VALUES (0,'$dt','$per','$usr','$less' ) ";
 if (!mysqli_query($con,$sql))
             {
                die('Error: ' . mysqli_error($con));	
	         }}
//.........................................................
?> 

<br /><br />
<a  href="index.php" style="color:blue "  >  Επιστροφή στο Μενού  </a>


</body>
</html>