<?php
session_start();
ob_start();
if ($_SESSION['islogin']=='' ) { 
  echo "ΠΡΕΠΕΙ ΝΑ ΕΓΓΡΑΦΕΙΤΕ ΓΙΑ ΝΑ ΧΡΗΣΙΜΟΠΟΙΗΣΕΤΕ ΤΟ ΣΥΣΤΗΜΑ"."<BR>";
  sleep(3);
  header("Location: loginall.php"); /* Redirect browser */
  exit();
  }
$menu = $_SESSION['menu'];
include $menu;
?>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid green;padding:2px;font-size:10pt; }
<p style="font-size:10px;"></p>

</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function popupCenter(pageURL, title, w, h) {
    var left = (screen.width / 2)  - (w / 2);
    var top  = (screen.height / 2) - (h / 2);
    var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800, height=700, top='+top+', left='+left);
}
</script>
<body>
<h2>ΑΠΟΡΟΙ ΜΑΘΗΤΕΣ/ΤΡΙΕΣ</h2>

<?php
require "dbconnect.php"; 
$dt=$_SESSION['dt'];
echo "Ημερομηνία: ".$dt;

$result=mysqli_query($con,"SELECT mitroo,tmima,onoma,eponimo from allstudentinfo order by tmima,eponimo");
$sqlx=mysqli_query($con,"SELECT mitroo,aporos from problimata where aporos='1'");
$aporoi=array();
$i=1;
while ($rowx = mysqli_fetch_array($sqlx))
{
$aporoi[$i]=$rowx['mitroo'];
$i++;	
}

?>
<h4> ΕΠΕΛΕΞΕ ΤΟΥΣ ΑΠΟΡΟΥΣ ΜΑΘΗΤΕΣ</h4>

<form  method='post' >
<div style="overflow:scroll; border:1px solid black;background:#F5F9F0;width:420px; height: 550px">
<?php
echo "<table style='width:400px' class='myTable' >
<tr style='border:1px solid black' bgcolor='#adcd82'  >
<th>ΜΗΤΡΩΟ</th>
<th>ΤΜΗΜΑ</th>
<th>ΜΑΘΗΤΗΣ / ΤΡΙΑ</th>
<th> </th>
<th>ΑΠΟΡΟΣ</th>
</tr>";

$i=1;
while ($rowr = mysqli_fetch_array($result)) 
  { 
    $stname[$i]=$rowr['eponimo']." ".$rowr['onoma'];
	$mitroo[$i]=$rowr['mitroo'];
	$tmima[$i]=$rowr['tmima'];
	if(in_array($mitroo[$i],$aporoi))
	{
	   $checked = "checked";
	}
	else
	{
		$checked = "";
    }
	echo "<tr>";
	echo "<td>".$mitroo[$i]."</td>";
	echo "<td>".$tmima[$i]."</td>";
	echo "<td>".$stname[$i]."</td>";
	echo "<td>"." "."</td>";
    echo "<td ALIGN=center>"."<input type='checkbox' name='aporos[]' value='$mitroo[$i]'  {$checked} >"."</td>";
	echo "</tr>";
    $i++;
}
echo "</table>";
?>
</div>
<br>
<input type="submit" name="aporos1" value="Επιβεβαίωση Επιλογών" >
</form>
<?php
if (isset($_POST['aporos1']))
{
	$aporos = $_POST['aporos'];
	$i=1;
	if (!empty($aporos))
	{
	    mysqli_query($con,"update problimata set aporos='0'");
		foreach ($aporos as $a)
	    {
	    	$ql=mysqli_query($con,"select count(mitroo) as cc from problimata where mitroo='$a'");
	    	$rowl=mysqli_fetch_array($ql);
	    	if ($rowl['cc'] > 0)
	    	{
	    		$q="update problimata set aporos='1' where mitroo='$a' ";
				if (!mysqli_query($con, $q)) 
				{
        			 echo "Error: " . $q . "<br>" . mysqli_error($con);
            
    			 }
	    	}
	    	else
	    	{
	    		$q="insert into problimata (mitroo,aporos) values('$a','1') ";
	    		if (!mysqli_query($con, $q)) 
				{
        			 echo "Error: " . $q . "<br>" . mysqli_error($con);
            
    			 }
	    	
	    
	    	}
		
		}
	}
	header("Location: simvaporoi.php");
}
?>	
<br />	<br />	
<a  href=<?php echo $menu;?> style="color:blue "  > <button> Επιστροφή στο Μενού   </a>
</body>
</html>