<?PHP
session_start();
if(isset($_SESSION['Nama']) and isset($_SESSION['Pass'])) {
$Nama = $_SESSION['Nama'];
if ($Nama == "tilang"){
?>
<?php
//koneksi ke database, username,password  dan namadatabase menyesuaikan 
mysql_connect('localhost', 'root', 'benk');
mysql_select_db('cma_tilang');
//memanggil file excel_reader
require "excel_reader.php";
//jika tombol import ditekan
if(isset($_POST['submit'])){
$prefix = $_POST["bulan"];
    $target = basename($_FILES['filepegawaiall']['name']) ;
    move_uploaded_file($_FILES['filepegawaiall']['tmp_name'], $target);
    $data = new Spreadsheet_Excel_Reader($_FILES['filepegawaiall']['name'],false);
//  menghitung jumlah baris file xls
    $baris = $data->rowcount($sheet_index=0);
//  jika kosongkan data dicentang jalankan kode berikut
    $drop = isset( $_POST["drop"] ) ? $_POST["drop"] : 0 ;
    if($drop == 1){
//  kosongkan tabel pegawai
             $truncate ="TRUNCATE TABLE tilang_$prefix";
             mysql_query($truncate);
    };
    
//    import data excel mulai baris ke-2 (karena tabel xls ada header pada baris 1)
    for ($i=2; $i<=$baris; $i++)
    {
//       membaca data (kolom ke-1 sd terakhir)
      $nomor           = $data->val($i, 1);
      $nomor_briva   = $data->val($i, 2);
      $nama  = $data->val($i, 3);
	  $alamat  = $data->val($i, 4);
$pasal  = $data->val($i, 5);
$bb  = $data->val($i, 6);
$denda = $data->val($i, 7);
$biayaperkara = $data->val($i, 8);
$tglsidang  = $data->val($i, 9);
$asaltilang  = $data->val($i, 10);
//  setelah data dibaca, masukkan ke tabel pegawai sql
      $query = "INSERT into tilang_$prefix (t_nomor,t_nomorbriva,t_nama,t_alamat,t_pasal,t_bb,t_denda,t_biayaperkara,t_tglsidang,t_asaltilang) values('$nomor','$nomor_briva','$nama','$alamat','$pasal','$bb','$denda','$biayaperkara','$tglsidang','$asaltilang')";
      $hasil = mysql_query($query);
    }
    if(!$hasil){
//          jika import gagal
          die(mysql_error());
      }else{
//          jika impor berhasil
         
		  print (' <table width="100%"  border="0" cellspacing="4" cellpadding="4" class="panel">
    <tr>
      <td bgcolor="#FF9999"><span class="style3">Data berhasil diimpor. Silahkan tutup jendela ini</span></td>
    </tr>
  </table>');
    }
    
//    hapus file xls yang udah dibaca
  //  unlink($_FILES['filepegawaiall']['name']);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>IMPORT DATA TILANG ANDA</title>
<LINK href="../images/index.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style3 {font-size: 36px}
-->
</style>
</head>

<body>
<form name="myForm" id="myForm" onsubmit="return validateForm()" action="index.php" method="post" enctype="multipart/form-data">
  <table width="100%"  border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td bgcolor="#CCCCCC"><table width="100%"  border="0" cellspacing="4" cellpadding="4">
        <tr>
          <td><strong>MASUKKAN DATA TILANG 10 KOLOM ANDA. HANYA FILE EXCEL EXTENSION .XLS YANG DAPAT DI IMPORT (EXCEL OFFICE 2003)</strong></td>
        </tr>
      </table></td>
    </tr>
  </table>
  <table width="100%"  border="0" cellspacing="2" cellpadding="2">
    <tr>
      <td width="25%">Masukkan data dalam Bulan </td>
      <td width="1%">:</td>
      <td width="74%"><select name="bulan" id="bulan">
          <option value="januari">Januari</option>
          <option value="februari">Februari</option>
          <option value="maret">Maret</option>
          <option value="april">April</option>
          <option value="mei">Mei</option>
          <option value="juni">Juni</option>
          <option value="agustus">Agustus</option>
          <option value="september">September</option>
          <option value="oktober">Oktober</option>
          <option value="nopember">Nopember</option>
          <option value="desember">Desember</option>
          <option value="0" selected="selected">--pilih bulan--</option>
      </select></td>
    </tr>
    <tr>
      <td>Cari File *.xls Excel 2003</td>
      <td>:</td>
      <td><input type="file" id="filepegawaiall" name="filepegawaiall" /></td>
    </tr>
    <tr>
      <td>Optional</td>
      <td>:</td>
      <td><input type="checkbox" name="drop" value="1" />
          <u>Kosongkan tabel sql terlebih dahulu.</u> </td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" value="Import" /></td>
    </tr>
  </table>
  <table width="100%"  border="0" cellspacing="4" cellpadding="4">
    <tr>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>CONTOH FILE EXCEL 2003 <a href="contoh.xls"><strong>KLIK DISINI </strong></a></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
    </tr>
  </table>
 
</form>
</body>
</html>
<?PHP
}
else {
echo "ANDA BELUM LOGIN, GAGAL AKSES";
echo "<meta http-equiv=\"refresh\" content=\"3; url=../index.php\">";
}}
?>