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

<html>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<head>

<style type="text/css">
table.myTable { border-collapse:collapse; }
table.myTable td, table.myTable th { border:1px solid black;padding:2px;font-size:14px; }
<p style="font-size:14px;"></p>
</style>
</head>
<body>

<?php
require "dbconnect.php"; 
$usr=$_SESSION['usr'];
if (isset($_POST['portdrasi']))
{
$mitroo = $_POST['mitr'];
$pdrasi=$_POST['pdrasi'];
$sql = mysqli_query($con,"SELECT  portfolio,paratirisi,mark FROM ddkportfolio WHERE code='$pdrasi' ");
$row = mysqli_fetch_array($sql);
$port=$row['portfolio'];
$mark=$row['mark'];
$para=$row['para'];
?>
<h3>ΚΑΤΑΧΩΡΗΣΗ ΔΔΚ  -  <?php echo $port;?> </h3>
<?php
echo '<p style="font-size: 14pt">' . "Μέγιστος Αρ. Μονάδων : "."<b>".$mark."</b>"." ".$para. '</p>';
echo "<br>";
echo "<table class='myTable' >
<tr style='border:1px solid black' bgcolor='#d6ff73'  >
<th>Α/Α</th>
<th>Μητρώο</th>
<th>Τμήμα</th>
<th>Ονοματεπώνυμο Μαθητή/τριας</th>
<th>Μονάδες</th>
</tr>";
?>
<form  method="post" > 
<?php
$i=1;
foreach($mitroo as $mit){
    $mitr[$i]=$mit;  
    $result = mysqli_query($con,"SELECT eponimo,onoma,tmima FROM allstudentinfo WHERE mitroo='$mit'" );    
    $row = mysqli_fetch_array($result);
    $name=$row['eponimo']." ".$row['onoma'];
    $field="f".$pdrasi;
	$res1 = mysqli_query($con,"SELECT ".$field." FROM ddk WHERE mitroo='$mit'");
    $row1=mysqli_fetch_array($res1); 
	$bathmos[$i]=$row1["$field"];
    echo "<td>" . $i. "</td>";
	echo "<td>" . $mitr[$i]. "</td>";
	echo "<td>" . $row['tmima']. "</td>";
	echo "<td>" . $name. "</td>";
	echo "<td>". "<input type='number' name='bathm[]' value='$bathmos[$i]' size='6' min='0' max='$mark' >" ."</td>";
    echo "</tr>";
	echo " <input type='hidden' name='mitro[]' value='$mitr[$i]'  >";
    $i++;

}
echo "</table>";
 
}
?>
<br>

<input type='text' name='pdrasi' value=<?php echo $pdrasi;?>  HIDDEN  >
<input type="Submit" name="Submitddk1" value="Καταχώρηση Βαθμολογιών ">
</form>

<?php

  if (isset($_POST['Submitddk1'])) 
  {
	$bathmos =$_POST['bathm'];
	$mitroo=$_POST['mitro'];
	$pdrasi=$_POST['pdrasi'];
	$field="f".$pdrasi;
	$i=1;
	$j=0;
	foreach( $mitroo  as $mitr ) 
	{
	   $result = mysqli_query($con,"SELECT * FROM ddk WHERE mitroo='$mitr'");
	   $c=mysqli_num_rows($result);
	   $dr=mysqli_num_fields($result)-1;
	
	   if ($c == 0) 
	   {
		for ($k=1;$k<=$dr;$k++)
		{
		    $key="f".$k; 
            $fields[$k] = "`$key`";
            $values[$k] = '0';
		}
	    $fieldsi = implode(",", $fields);
        $valuesi = implode(",", $values);
	    $sql= "INSERT INTO ddk (mitroo,$fieldsi) VALUES ('$mitr',$valuesi)" ;   
	    if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));} 
		
	   }		
       $sql= "UPDATE  ddk  SET ".$field." = '$bathmos[$j]' WHERE mitroo='$mitr'" ;
       if (!mysqli_query($con,$sql)){die('Error: ' . mysqli_error($con));    } 
	   /////////
	   $sql= mysqli_query($con,"select * from ddkteach where pmp='$usr' and mitroo='$mitr' and pcode='$pdrasi' " ) ;
	   if (mysqli_num_rows($sql) ==0)
	   {
	     $sql1= "INSERT INTO ddkteach (pmp,mitroo,pcode,bathmos,date) VALUES ('$usr','$mitr','$pdrasi','$bathmos[$j]','$dt')" ;   
	     if (!mysqli_query($con,$sql1)){die('Error: ' . mysqli_error($con));} 
	   }
	   else
	   {
		 $sql1= "update ddkteach set bathmos='$bathmos[$j]',date='$dt' where pmp='$usr' and mitroo='$mitr' ";   
	     if (!mysqli_query($con,$sql1)){die('Error: ' . mysqli_error($con));}   
	   }
	   /////////
	   $j++;
   }
  header("Location:ddkdraseis.php");
}

?>
<br /><br /><br />
<a  href=<?php echo $menu; ?>  style="color:blue "  >  Επιστροφή στο Μενού  </a>

</body>
</html>