<?php
session_start();
$lnk_url = $_SERVER['HTTP_REFERER'];
date_default_timezone_set('UTC');
include_once('connection.php');


$limitSize	= 10485760; //(10 MB) - Maximum size of uploaded file, change it to any size you want
$fileName	= basename($_FILES['file']['name']);
$fileSize	= $_FILES["file"]["size"];
$fileExt	= substr($fileName, strrpos($fileName, '.') + 1);

if((empty($_FILES["file"])) && ($_FILES['file']['error'] == 0))
{
	$_SESSION['error_ph_upload'] = "<span style=\"font-size: 8pt; color:#FF0000;\">Error! Upload a *.xlsx file.</span>";
	header("Location: $lnk_url");
}
elseif($fileExt != "xlsx")
{
	$_SESSION['error_ph_upload'] = "<span style=\"font-size: 8pt; color:#FF0000;\">Error! Upload a *.xlsx file only.</span>";
	header("Location: $lnk_url");
}
elseif($fileSize > $limitSize)
{
	$_SESSION['error_ph_upload'] = "<span style=\"font-size: 8pt; color:#FF0000;\">Error! File size( $fileSize Bytes ) is greater than the mentioned size( 10MB / 10485760 Bytes ).</span>";
	header("Location: $lnk_url");
}
elseif($fileSize < 1)
{
	$_SESSION['error_ph_upload'] = "<span style=\"font-size: 8pt; color:#FF0000;\">Error! Upload a proper *.xls file.</span>";
	header("Location: $lnk_url");
}
else
{

/*
<style>
<!---
#datacontent{margin-left:20pt}
#xlsxTable{font-family: "Arial";font-size: 7pt;margin: 15px;text-align: left;border-collapse: collapse;}
#xlsxTable th{padding: 3px;font-weight: bold;font-size: 7pt;color:#FFFFFF;background: #b9c9fe; border-right: 1px solid #fff;}
#xlsxTable td{padding: 3px;background: #e8edff;border-top: 1px solid #fff; border-right: 1px solid #fff; color: #669;}
#xlsxTable tbody tr:hover td{background: #d0dafd;}
//--->
</style>
*/


	// ------- Truncate table ----------------
	$sql_truncate = "TRUNCATE TABLE `data_store`";
	$res_truncate = mysqli_query($conn,$sql_truncate);
	// ---------------------------------------
	
	require_once "simplexlsx.class.php";

	$getWorksheetName = array();
	$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );
	$getWorksheetName = $xlsx->getWorksheetName();
	/*echo '	<div id="datacontent">
			<p align="center">
			<a href="index.php" title="Back to upload file" style="font-family:arial; font-size:10pt; color:#990000; font-weight:bold; text-decoration:none;">Back</a></p>
			<p align="center" style="font-family:arial; font-size:10pt; color:#FF0000; font-weight:bold;"><u>Data Uploaded Successfully</u></p>
	';
	echo '<p style="font-family:arial; font-size:9pt; color:#990000; font-weight:bold;"><u>File Info::</u></p><ul>';
	echo '<li style="font-family:arial; font-size:8pt; color:#990000;"><b>File Name :</b> '.$fileName.'</li>';
	echo '<li style="font-family:arial; font-size:8pt; color:#990000;"><b>File Size :</b> '.($fileSize/1000).' kb</li></li>';
	echo '</ul>
	<p style="font-family:arial; font-size:9pt; color:#990000; font-weight:bold;"><u>Worksheets::</u></p><ul>';*/
	
	foreach ($getWorksheetName as $value) {
		//echo '<li style="font-family:arial; font-size:8pt; color:#990000;">'.$value.'</li>';
	}
	//echo '</ul>';
	
	//echo '<p style="font-family:arial; font-size:9pt; color:#990000; font-weight:bold;"><u>File data in table format::</u></p>
	//<div id="datacontent">';
	
	for($j=1;$j <= $xlsx->sheetsCount();$j++){
		//echo '<p style="font-family:arial; font-size:8pt; color:#990000;"><b>Worksheet Name :</b> '.$getWorksheetName[$j-1].'</p>';
		//echo '<table id="xlsxTable">';
		list($cols,) = $xlsx->dimension($j);
		
		//Prepare table
		foreach( $xlsx->rows($j) as $k => $r) {
			
			if ($k == 0){
				$trOpen		= '<th';
				$trClose	= '</th>';
				$tbOpen		= '<thead>';
				$tbClose	= '</thead>';
			}else{
				$trOpen		= '<td';
				$trClose	= '</td>';
				$tbOpen		= '<tbody>';
				$tbClose	= '</tbody>';
			}
			//echo $tbOpen;
			//echo '<tr>';
			
			for( $i = 0; $i < $cols; $i++)
			{
				//Display data
				//echo $trOpen.'>'.( (isset($r[$i])) ? $r[$i] : '&nbsp;' ).$trClose;
			}				
			//echo '</tr>';
			//echo $tbClose;
			
			// -------- Inser data into mySql Database ----------------
			$slno = escape($r[0]);
			$item = escape($r[1]);
			$make = escape($r[2]);
			$size = escape($r[3]);
			$width = escape($r[4]);
			$price = escape($r[5]);
			
			if(($item == 'ITEM') && ($make == 'MAKE ')){}else{
				if(($item != '') && ($make != '')){
					$sqls="INSERT INTO `data_store` (`slno`,`item`,`make`,`size`,`width`,`price`) 
					values('$slno','$item','$make','$size','$width','$price')";	
					$results = mysqli_query($conn,$sqls) or die('Error, Query failed : ' . mysqli_error($conn));
				}
			}
			// ----------------- End ---------------------------------
			
		}
		//echo '</table>';
	}
	//echo '</div>';
	//echo '	</div>';
	$_SESSION['error_ph_upload'] = "<span style=\"font-size: 8pt; color:#FF0000;\">Data uploaded.</span>";
header("Location: upload.php");

}
	
function escape($string) {
	return htmlspecialchars($string, ENT_QUOTES);
}

?>