<?php
include('config.php'); 
include "PHPExcleReader/Classes/PHPExcel/IOFactory.php";
$uploadedStatus = 0;
if ( isset($_POST["Submit"]) ) { if ( isset($_FILES["excelfile"])) { 
//if there was an error uploading the file 
if ($_FILES["excelfile"]["error"] > 0) { 
echo "Return Code: " . $_FILES["excelfile"]["error"] . "<br />"; }
else { //if (file_exists($_FILES["excelfile"]["name"])) { unlink($_FILES["excelfile"]["name"]); }


$storagename = "upload_excel/".basename($_FILES['excelfile']['name']);
move_uploaded_file($_FILES["excelfile"]["tmp_name"], $storagename); $uploadedStatus = 1;} }
else { echo "No file selected <br />"; } }

if($uploadedStatus==1){
	try {
		$objPHPExcel = PHPExcel_IOFactory::load($storagename);
	} catch(Exception $e) {
		die('Error loading file "'.pathinfo($storagename,PATHINFO_BASENAME).'": '.$e->getMessage());
	}
	$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
	//print_r($sheetData);
	$delete="delete from tbl_statement";
	$res=mysqli_query($conn,$delete);
	$sql_query = "INSERT INTO tbl_statement(`emp_acc`, `emp_name`,`empl_opg_bal`,`empl_subsc`,`empl_total`,`empl_withdrawals`, `empl_bal`, `empl_int`,`empl_tot`,`emplyr_opg_bal`,`emplyr_cntr`,`emplyr_total`,`emplyr_withdrawls`,`emplyr_bal`,emplyr_int,emplyr_tot,loan_outstndng,`emp_bankname`,`emp_accno`,`emp_uan`) VALUES";
	$c=$r=0;
	foreach ($sheetData as $row) {
		$c++;
		if($c>1 && $row['A']!=""){
			$r++;
			
		
		 $sql_query .= " ('".trim($row['A'])."','".trim($row['B'])."','".trim($row['C'])."','".trim($row['D'])."','".trim($row['E'])."','".trim($row['F'])."','".trim($row['G'])."','".trim($row['H'])."','".trim($row['I'])."','".trim($row['J'])."','".trim($row['K'])."','".trim($row['L'])."','".trim($row['M'])."','".trim($row['N'])."','".trim($row['O'])."','".trim($row['P'])."','".trim($row['Q'])."','".trim($row['R'])."','".trim($row['S'])."','".trim($row['T'])."'),";
		}
	   
	}
	$sql_query=rtrim($sql_query, ",");
	//echo $sql_query;
	// $Dsql="TRUNCATE TABLE vouchers_temp";
	 //mysqli_query($con,$Dsql) or die("MySQL Error1: " . mysqli_error($con));
	 mysqli_query($conn,$sql_query) or die("MySQL Error2: " . mysqli_error($connection));
	// header("Location: vouchers_temp.php?import&c=$r") ;

	//echo "<h1> Successfully imported $c records </h1>";
	echo'<script language=javascript>alert("Import Successfull");</script>';
 echo'<script type="text/javascript">window.location="import.php";</script>';
}
?>