<?php    
    if(isset($_POST['upload'])){
        $fileName = $_FILES['file']['name'];
        $fileTmpName = $_FILES['file']['tmp_name'];
        $fileTmpNamex = $_FILES['file']['tmp_name'];
        //find extension of file
        $fileExtension = pathinfo($fileName,PATHINFO_EXTENSION);
        //define allowed extension
        $allowedType = array('csv');
        if(!in_array($fileExtension, $allowedType)){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Invalide file extension....only excel documents of .csv extension are allowed</span>
                </div>';
        }else{
            $suces='';
            $rowx=$row=1;
            //upload file
            $handlex = fopen($fileTmpNamex,'r');//read mode 'r'
            $handle = fopen($fileTmpName,'r');//read mode 'r'
            //fgetcsv($handle, 1000, ",");
            
            //check if exam results already exists
            while(($my_data = fgetcsv($handlex, 1000, ",")) !== FALSE){
               $rowx++;
               
                if($rowx>=11){
                    $exam_code =  isset($my_data [1]) ? $my_data [1] : null;
                    $unix = isset($my_data [3]) ? $my_data [3] : null;
                    $yearx = isset($my_data [4]) ? $my_data [4] : null;
                    $semisterx = isset($my_data [5]) ? $my_data [5] : null;
                    $stux = isset($my_data [6]) ? $my_data [6] : null;
                    $categoryx = isset($my_data [7]) ? $my_data [7] : null;
                }
            }
            if(!isset($exam_code)){
                echo'<div class="alert alert-warning absolue center text-center" role="alert">
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                        <span class="text-warning"><b>Failed to identify exams code..Ensure you are uploading the right .csv excel document!</b></span>
                    </div>';
            }else
            {
                
                $sel = mysql_query("SELECT * FROM results WHERE code='$exam_code'");
                $countx=  mysql_num_rows($sel);
            
                if($countx>0){
                    echo'<div class="alert alert-warning absolue center text-center" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
                            <span class="text-warning" style="font-weight:bold;">Results for this exam/practical already exists...Try downloading another exam results sheet!</span>
                        </div>';
                }else{
                    
                    //continue if exam results do not exist
                    while(($myData = fgetcsv($handle, 1000, ",")) !== FALSE){
                      $row++;

                        if($row>=11){
                            $code =  isset($myData[1]) ? $myData[1] : null;
                            $lec = isset($myData[2]) ? $myData[2] : null;
                            $uni = isset($myData[3]) ? $myData[3] : null;
                            $year = isset($myData[4]) ? $myData[4] : null;
                            $semister = isset($myData[5]) ? $myData[5] : null;
                            $stu = isset($myData[6]) ? $myData[6] : null;
                            $category = isset($myData[7]) ? $myData[7] : null;
                            $mrks = isset($myData[8]) ? $myData[8] : null;
                            $score = isset($myData[10]) ? $myData[10] : null;

                                if(!$insert = mysql_query("INSERT INTO results (category,code,total,added,unitID,studentID,marks,lec,staffID,added_by) VALUES ('$category','$code','$mrks',NOW(),'$uni','$stu','$score','$lec','$staffID','$stafNo')"))
                                {
                                    echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                                <span aria-hidden="true">×</span>
                                            </button>
                                            <span class="text-danger">'.mysql_error().'</span>
                                        </div>';
                                }
                                else{
                                    $suces='true';
                                }
                        }
                    }
                    if($suces=='true'){
                        $_SESSION["new"]='True';
                        header('Location:exam-results.php');
                        echo "<script type='text/javascript'> document.location = 'exam-results.php'; </script>";
                        exit();
                    }
                }
            }
        }
    }
        
?>                                                             