<?php
require("../../config/koneksi.php");
require("../../config/function.php");
require("../../config/crud.php");
require_once("../../PHPExcel/PHPExcel.php");
session_start();
$kelas=$_POST['kelas'];
$mapel=$_POST['mapel'];
	
$jml = mysqli_num_rows(mysqli_query($koneksi, "SELECT * FROM deskripsi 
WHERE kelas='$kelas' AND mapel='$mapel' AND  ki='KI3' "));	

 $nama_file_baru = 'data.xlsx';
	
				if(is_file('../../temp/'.$nama_file_baru)) 
					unlink('../../temp/'.$nama_file_baru); 

				$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION); 
				$tmp_file = $_FILES['file']['tmp_name'];
				if($ext == "xlsx"){
					move_uploaded_file($tmp_file, '../../temp/'.$nama_file_baru);
     
	$excelreader = new PHPExcel_Reader_Excel2007();
	$loadexcel = $excelreader->load('../../temp/'.$nama_file_baru); 
	$sheet = $loadexcel->getActiveSheet()->toArray();
	
	
	 $nama_file_baru = 'data.xlsx';
        $sukses = $gagal = 0;
       
        for ($a = 4; $a < (4+$jml+2); $a++) {
            $kode = $sheet['0'][$a];
            
            for ($i = 1; $i < count($sheet); $i++) {
				

                $nis = $sheet[$i]['1'];
				$mapel = $sheet[$i]['3'];
				$kelas = $sheet[$i]['14'];
				$ki = $sheet[$i]['15'];
				$smt = $sheet[$i]['16'];
				$tahun = $sheet[$i]['17'];
                $kode = $kode;
                $nilai = $sheet[$i][$a];
				
              
				
	
$query = "INSERT INTO nilai_rapor(nis,mapel,nilai,ket,kelas,ki,smt,tahun) VALUES('".$nis."','".$mapel."','".$nilai."','".$kode."','".$kelas."','".$ki."','".$smt."','".$tahun."')";

		
			mysqli_query($koneksi, $query);
			
            }
        }
}

