<?php
if (isset ( $_POST ['submit'] )) {
	
	$secondtable = 'DESCRIBE `first_salaryslip`';
	$totaldata_secondtable = $db->run ( $secondtable )->fetchAll ();
	
	foreach ( $totaldata_secondtable as $row1 ) {
		if ($row1 ['Field'] != 'id') {
			$pastname1 = $row1 ['Field'];
			$_POST [$pastname1];
			$currentname1 = preg_replace ( '/[^a-zA-Z0-9\']/', '_', $_POST [$pastname1] );
			
			$query1 = $db->run ( "alter table `first_salaryslip` change `$pastname1` `$currentname1` varchar(255)" );
			
			if (! $query1) {
				$secondname .= $currentname1 . ' ';
			}
		
		}
	}
	
	$thirdtable = 'DESCRIBE `second_salaryslip`';
	$totaldata_thirdtable = $db->run ( $thirdtable )->fetchAll ();
	
	foreach ( $totaldata_thirdtable as $row2 ) {
		if ($row2 ['Field'] != 'id') {
			$pastname2 = $row2 ['Field'];
			$currentname2 = preg_replace ( '/[^a-zA-Z0-9\']/', '_', $_POST [$pastname2] );
			
			$query2 = $db->run ( "alter table `second_salaryslip` change `$pastname2` `$currentname2` varchar(255)" );
			
			if (! $query2) {
				$thirdname .= $currentname2 . ' ';
			}
		
		}
	}
	
	// ---------------------------------------------------AT LAST FINALY ERROR
	// OR SAVED MESSAGE SHOW------------------->
	
	if ($secondname == '' && $thirdname == '') {
		?>
<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Inserted..</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
} else {
		?>

<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Is Some Problem In Inserting in this fields Or Fields Are Remain Same. 
					<?php
		if ($firstname != '') {
			$error .= '"' . $firstname . '"' . ',';
		}
		if ($secondname != '') {
			$error .= '"' . $secondname . '"' . ',';
		}
		if ($thirdname != '') {
			$error .= '"' . $thirdname . '"' . ',';
		}
		if ($fourthname != '') {
			$error .= '"' . $fourthname . '"' . ',';
		}
		echo rtrim ( $error, ',' );
		?>
				</h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
}
}
?>
<!-- --------------------------------------------------SECOND SUBMIT------------------------------------------- -->
<?php
if (isset ( $_POST ['submit1'] )) {
	$countfirsttable = $db->get_count ( "first_salaryslip" );
	$countthirdtable = $db->get_count ( "second_salaryslip" );
	
	if ($countfirsttable == 1) {
		$firstsalarydata = $db->get_all ( "first_salaryslip" );
		
		foreach ( $firstsalarydata [0] as $value => $detect ) {
			echo $value;
			echo '<br>';
			if ($value != 'id' && $value != 'earning') {
				$fieldname = "a_" . $value;
				$field_val = $_POST [$fieldname];
				
				if (strpos ( $field_val, '%' ) !== false) {
					$detect_current = explode ( "%", $field_val );
					$percentvalue [] = $detect_current [0] . ',';
				}
			}
			if (strpos ( $detect, '%' ) !== false) {
				$detect = explode ( "%", $detect );
				$firsttablepercent [] = $detect [0] . ',';
			}
		}
	}
	if ($countthirdtable == 1) {
		$secondsalarydata = $db->get_all ( "second_salaryslip" );
		foreach ( $secondsalarydata [0] as $value => $detect1 ) {
			if ($value != 'id' && $value != 'earning') {
				$fieldname1 = "a_" . $value;
				$field_val1 = $_POST [$fieldname1];
				
				if (strpos ( $field_val1, '%' ) !== false) {
					$detect_current1 = explode ( "%", $field_val1 );
					$percentvalue1 [] = $detect_current1 [0] . ',';
				}
			}
			
			if (strpos ( $detect1, '%' ) !== false) {
				$detect1 = explode ( "%", $detect1 );
				$secondtablepercent [] = $detect1 [0] . ',';
			}
		}
	}
	if ($firsttablepercent != '') {
		$firstdata = array_sum ( $firsttablepercent );
	}
	if ($secondtablepercent != '') {
		$second_data = array_sum ( $secondtablepercent );
	}
	if ($percentvalue != '') {
		$percentsum = array_sum ( $percentvalue );
	}
	if ($percentvalue1 != '') {
		$percentsum1 = array_sum ( $percentvalue1 );
	}
	
	echo $total = $firstdata + $second_data;
	echo "<br>";
	echo $currentsum = $percentsum + $percentsum1;
	
	// -------------------------------------In Second Table Data Start
	
	/*
	 * if($countfirsttable !=1) { $secondtable= 'DESCRIBE `first_salaryslip`';
	 * $totaldata_secondtable= $db->run($secondtable)->fetchAll(); $a=1; foreach
	 * ($totaldata_secondtable as $row1) { if($row1['Field'] != 'id' &&
	 * $row1['Field'] != 'earning') { $columnname1= $row1['Field']; $postvalue1=
	 * "a_".$row1['Field']; // special character are removed by '_'
	 * $currentname1= $_POST[$postvalue1]; if($currentname1 == '') {
	 * $currentname1= ""; } else { if ($a ==3) { $query=
	 * $db->insert("first_salaryslip", array($columnname1=>$currentname1)); if
	 * ($query) { //get the last insert id $getid2= $db->lastInsertId(); } }
	 * else { $query1= $db->update("first_salaryslip",
	 * array($columnname1=>$currentname1), array("id"=>$getid2)); if (!$query1)
	 * { $seconderror .= $columnname1.' '; } } } } $a++; } } else {
	 * $secondtable= 'DESCRIBE `first_salaryslip`'; $totaldata_secondtable=
	 * $db->run($secondtable)->fetchAll(); foreach ($totaldata_secondtable as
	 * $row1) { if($row1['Field'] != 'id' && $row1['Field'] != 'earning') {
	 * $columnname1= $row1['Field']; $postvalue1= "a_".$row1['Field']; //
	 * special character are removed by '_' $currentname1= $_POST[$postvalue1];
	 * if($currentname1 == '') { $currentname1= ""; } else { $query1=
	 * $db->update("first_salaryslip", array($columnname1=>$currentname1),
	 * array("id"=>1)); if (!$query1) { $seconderror .= $columnname1.' '; } } }
	 * } } //-------------------------------------In Third Table Data Start
	 * if($countthirdtable !=1) { $thirdtable= 'DESCRIBE `second_salaryslip`';
	 * $totaldata_thirdtable= $db->run($thirdtable)->fetchAll(); $a=1; foreach
	 * ($totaldata_thirdtable as $row3) { if($row3['Field'] != 'id' &&
	 * $row1['Field'] != 'deductions') { $columnname3= $row3['Field'];
	 * $postvalue3= "a_".$row3['Field']; // special character are removed by '_'
	 * $currentname3= $_POST[$postvalue3]; if($currentname3 == '') {
	 * $currentname3= ""; } else { if ($a ==3) { $query4=
	 * $db->insert("second_salaryslip", array($columnname3=>$currentname3)); if
	 * ($query4) { //get the last insert id $getid3= $db->lastInsertId(); } }
	 * else { $query5= $db->update("second_salaryslip",
	 * array($columnname3=>$currentname3), array("id"=>1)); if (!$query5) {
	 * $thirderror .= $columnname3.' '; } } } } $a++; } } else { $thirdtable=
	 * 'DESCRIBE `second_salaryslip`'; $totaldata_thirdtable=
	 * $db->run($thirdtable)->fetchAll(); foreach ($totaldata_thirdtable as
	 * $row3) { if($row3['Field'] != 'id' && $row1['Field'] != 'deductions') {
	 * $columnname3= $row3['Field']; $postvalue3= "a_".$row3['Field']; //
	 * special character are removed by '_' $currentname3= $_POST[$postvalue3];
	 * if($currentname3 == '') { $currentname3= ""; } else { $query5=
	 * $db->update("second_salaryslip", array($columnname3=>$currentname3),
	 * array("id"=>1)); if (!$query5) { $thirderror .= $columnname3.' '; } } } }
	 * }
	 */
	
	// ---------------------------------------------------AT LAST FINALY ERROR
	// OR SAVED MESSAGE SHOW------------------->
	
	// if ( $seconderror =='' && $thirderror =='' )
	{
		?>
<div id="hide">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-1" class="gritter-item-wrapper gritter-success"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">

				<div class="gritter-without-image">
					<span class="gritter-title">Cheers..
						<div class="gritter-close" id="gritterremove"
							style="display: inline;"></div>
					</span>
					<p>Your Record Is Inserted..</p>
				</div>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
}
	// else
	{
		?>
<div class="hide1">
	<div id="gritter-notice-wrapper">
		<div id="gritter-item-2" class="gritter-item-wrapper gritter-warning"
			style="">
			<div class="gritter-top"></div>
			<div class="gritter-item">
				<div class="gritter-close" id="gritterremove"
					style="display: inline;"></div>
				<h5>Oops! There Is Some Problem In Inserting in this fields<?php echo $firsterror.','.$seconderror.','.$thirderror.','.$fourtherror."...";  ?></h5>
				<div style="clear: both"></div>
			</div>
			<div class="gritter-bottom"></div>
		</div>
	</div>
</div>
<?php
	
}

}
?>