<?php
require('../../../assets/lib/ExcelImport/php-excel-reader/excel_reader2.php');
require('../../../assets/lib/ExcelImport/SpreadsheetReader.php');
require"../../../../inc/conn.php";

$id_kelas   = (isset($_POST['id_kelas'])) ? $_POST['id_kelas'] : "";


$mimes = ['application/vnd.ms-excel','text/xls','application/vnd.oasis.opendocument.spreadsheet','application/octet-stream'];
if(in_array($_FILES["file"]["type"],$mimes)){


$uploadFilePath = '../../../upload/tmpdatasiswa.xls';
move_uploaded_file($_FILES['file']['tmp_name'], $uploadFilePath);


    $data = new Spreadsheet_Excel_Reader($uploadFilePath);
    $baris = $data->rowcount();
    

    for($i=3;$i<=$baris;$i++){

      $nama_lengkap1      = $data->val($i,2);
      $username1          = $data->val($i,3);
      $password1          = $data->val($i,4);

      


      $nama_lengkap   = str_replace("'","`",(isset($nama_lengkap1)) ? $nama_lengkap1 : "");
      $username       = str_replace("'","`",(isset($username1)) ? $username1 : "");
      $password       = str_replace("'","`",(isset($password1)) ? $password1 : "");
      
      
        $simpan = mysqli_query($connect,"insert into tb_siswa set 
          id_kelas      = '".$id_kelas."',
          nama_lengkap  = '".$nama_lengkap."',
          username      = '".$username."',
          password      = '".$password."'
        ");


    }
    echo"<script>alert('Import berhasil !');</script>";
    echo"<script>document.location.href='index.php?prefix=datasiswa&kelas=$id_kelas'</script>";


}else{ 
  echo"<script>alert('Gagal mengeksekusi perintah ini !');history.go(-1)</script>";
}
?>