<?php
$errmsg='';
$sucmsg='';
if(isset($_POST['add'])){
    extract($_POST);
    $errmsg='';
    $sucmsg='';
    
    //get course count
    $q = "select * from tpc_course";
    $r = mysqli_query($con, $q);
    $coursecount = mysqli_num_rows($r);

	//update record
		
   for($i=1; $i<=$coursecount; $i++)
   {
	$gname_update = "course".$i;
	$mycourse = "course".$i;
	
	if(isset($_POST[$gname_update])){
		//echo "<br>Course ".$i." = ".$_POST[$gname_update];
		if($_POST[$gname_update] == "on"){ $newvalue = "1"; }else{ $newvalue = "0";}
	}else{ $newvalue = "0";}
	
		$qry2=mysqli_query($con, "update tpcr_lastreg SET $mycourse='$newvalue', examNo='$examNo', examCentre='$examCentre', examYear='$examYear' where applicant_id='$applicant_id'");
		$sucmsg = 'Record updated successfully';
   }
}


?>

<div class="graph-visual tables-main">
<h2 class="inner-tittle"> EXAMINATION WHERE YOU LAST ATTEMPTED THE REFERENCE(S)  </h2>
	<div class="graph-form">
    		<div class="form-body">
			
    			<form method="post" name="lastregform" action="index.php?page=lastreg&i=<?php echo $_GET['i']; ?>"> 
                <?php 
                if($errmsg!=''){
                    echo '<div class="alert alert-danger">'.$errmsg.'</div>';
                }elseif($sucmsg!=''){
                    echo '<div class="alert alert-success">'.$sucmsg.'</div>';
                }
                
                ?>
                    <div class="form-group"> 

                        <?
                        //get exam info
			$q2b = "select * from tpcr_lastreg where applicant_id=$applicant_id";
			$r2b = mysqli_query($con, $q2b);
			$row2b = mysqli_fetch_array($r2b);
			$examNo = $row2b["examNo"];
			$examYear = $row2b["examYear"];
			$examCentre = $row2b["examCentre"];
			

                        ?>
                        <label>Examination Number</label> 
                        <input type="text" class="form-control" name="examNo" value="<?php echo $examNo; ?>" required=""> 
                    	</div>
                    	<div class="form-group"> 
                        <label>Examination Centre</label> 
                        <input type="text" class="form-control" name="examCentre" value="<?php echo strtoupper($examCentre); ?>" required=""> 
                    	</div>
                    	<div class="form-group"> 
                        <label>Examination Year</label> 
                        <select class="form-control" name="examYear" style="width:100%; height:auto" required="">
                         <?php echo "<option>$examYear</option>"; $y=date('Y'); for($iy=$y; $iy>1960; $iy--){ echo "<option>$iy</option>"; } ?>
                        </select>
                    	</div>
                    
                    <div class="form-group"> 
                    
                        <label>TPC Courses</label> 
						<br> Tick as appropriate
                        <?php
						 $q = "select * from tpc_course";
						 $r = mysqli_query($con, $q);
						 $coursecount = mysqli_num_rows($r);
						 
						 echo "<table border='0'>";
						 $x = 1;
						while($row=mysqli_fetch_assoc($r))
						{
							$gname = "course".$x;
							extract($row);
							
							
							//check old option
							$q2 = "select * from tpcr_lastreg where applicant_id=$applicant_id";
							$r2 = mysqli_query($con, $q2);
							$row2 = mysqli_fetch_array($r2);
							$examNo = $row2["examNo"];
							$examYear = $row2["examYear"];
							$examCentre = $row2["examCentre"];
							
							//$mycell = "course".$x;
							$sel='';
							if($row2[$gname]=="1"){ $sel = "checked"; }
						
							
							echo '<tr><td> <input type="checkbox" name="'.$gname.'" style="height:30px; padding:0px; width:30px" '.$sel.' />';
							echo '&nbsp; &nbsp; <td>'.strtoupper($course);
							
							echo '</td></tr>';
							$x++;
						}
							echo "</table>";
						 
						 
						 
						 
						 
						
						?>
				</div>
				
						
                    <input name="add" type="submit" class="btn btn-default" value="Update Course"/> 
                </form> 
    		</div>
    
    </div>

</div>

<div>

<?php

echo "<a href='?page=firstreg&i=".$_GET['i']."'><button class='btn'>Back</button></a>";

 if(!empty($examNo)){ echo "<a href='?page=currentreg&i=".$_GET['i']."'><button class='btn'>Next</button></a>"; }
 
 ?>

</div>
