Hi,
I am quite new to php and am having some difficulties getting this script working.
What i want to happen is this:
The user enters this page with the id passed in the url, if the DateStarted is already set/valid then the script must have been previously run and no update/ function is run/executed. If the DateStarted is not set then the date is entered and assuming its the correct format the calculate button runs the functions to calculate the bonuses and these are updated to the Bonuses table and displayed in the text fields. Any help would be MUCH appreciated!
<?php
$pageTitle = 'Employees work related bonuses';
include 'header.html';
'<br />';
echo '<h1>Employees Bonuses</h1>';
if ((isset($_GET['ID'])) && (is_numeric($_GET['ID']))) {
$ID = $_GET['ID'];
}
elseif ((isset($_POST['ID'])) && (is_numeric($_POST['ID']))) {
$ID = $_POST['ID'];
}
else {
echo '<p>Error accessing the page</p>';
include 'footer.html';
exit();
}
require_once('mysqliConn.php');
$currentDate = date('Y-m-d');
function daysDifference($endDate, $beginDate) {
$date_parts1=explode("-", $beginDate);
$date_parts2=explode("-", $endDate);
$start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
return $end_date - $start_date;
}
$startLeave = 25;
function calcBonuses() {
if ($years < 0.5) {
$prob = 'yes';
}
else {
$prob = 'no';
}
if ($years > 1) {
$pen = 'yes';
}
else {
$pen = 'no';
}
if ((1 < $years) && ($years < 2)) {
$spt = 'SSP';
}
elseif ((2 < $years) && ($years < 3)) {
$spt= 'SP1+1';
}
elseif ($years > 3) {
$spt = 'SP2+2';
}
else {
$spt = 'NONE';
}
switch ($years) {
case($years < 5) :
case((5 <= $years) && ($years < 6)) :
$leaveDays = $startLeave + 1;
case((6 <= $years) && ($years < 7)) :
$leaveDays = $startLeave+ 2;
case((7 <= $years) && ($years < 8)) :
$leaveDays = $startLeave + 3;
case((8 <= $years) && ($years < 9)) :
$leaveDays = $startLeave + 4;
case($years >= 9) :
$leaveDays = $startLeave + 5;
}
}
$dateParts=explode("-", $startDate);
if ((isset($_POST['calculated'])) && (isset($startDate))) {
$startDate = $_POST['StartDate'];
$years = ((daysDifference($currentDate,$startDate)) / 365.25);
calcBonuses();
$q = "UPDATE Bonuses SET StartDate='$StartDate' , Probation='$prob', PensionEntitled='$pen', SickPayType='$spt', TotalLeaveDays='$leaveDays'
WHERE EmployeeID=$ID LIMIT 1";
$r= @mysqli_query($dbc, $q);
if (mysqli_affected_rows($dbc) == 1) {
echo '<p>The Employees bonuses have been edited</p>';
}
else {
echo '<p>Please enter a start date and press calculate!</p>';
}
}
$q = "SELECT Bonuses.FirstName, Bonuses.Surname, Bonuses.EmployeeID, StartDate, Probation, PensionEntitled, SickPayType, TotalLeaveDays, TotalLeaveHours
FROM Bonuses JOIN Employee ON Employee.EmployeeID = Bonuses.EmployeeID WHERE Bonuses.EmployeeID=$ID ";
$r = @mysqli_query($dbc, $q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
?>
<form action="empBonuses.php" method="post">
<p>First Name: <input type="text" name="FirstName" size="20" readonly="readonly" value="<?php echo $row['FirstName']; ?>"></p>
<p>Surname: <input type="text" name="Surname" size="20" readonly="readonly" value="<?php echo $row['Surname']; ?>"></p>
<p>Start Date: <input type="text" name="StartDate" size="10" value= "<?php echo $row['StartDate']; ?>"> (YYYY-MM-DD)</p>
<p>Probation? <input type="text" name="Probation" size="5" readonly="readonly" value="<?php if (isset($prob)) { echo $prob ; } else { echo $row['Probation']; }?>"></p>
<p>Pension Entitled: <input type="text" name="PensionEntitled" readonly="readonly" size="5" value="<?php if (isset($pen)) { echo $pen; } else { echo $row['PensionEntitled']; }?>"></p>
<p>Sick Pay Type: <input type="text" name="SickPayType" size="5" readonly="readonly" value="<?php if (isset($spt)){ echo $spt; } else { echo $row['SickPayType']; }?>"></p>
<p>Total Leave Days: <input type="text" name="TotalLeaveDays" readonly="readonly" size="5" value="<?php if (isset($leaveDays)) { echo $leaveDays; } else { echo $row['TotalLeaveDays']; }?>"></p>
<p>Total Leave Hours: <input type="text" name="TotalLeaveHours" readonly="readonly" size="5" value="<?php echo $row['TotalLeaveHours']; ?> "></p>
<input type="hidden" name="calculated" value="TRUE"/>
<input type="hidden" name="ID" value="<?php echo $ID; ?>"/>
<input type="submit" name="submit" value="Calculate"/>
</form>
<?php
}
mysqli_close($dbc);
include 'footer.html';
?>