I wrote a simple program which calculates bonus for amount of work done. But at the end of the month where I need to make report I encountered strange behaviour of PHP calculating it. The goal is to give 0 bonus when work is under 30, give 30% between 30 and 40, and above 40 give 50%. But very strange thing appears when I execute the code.
For example: data1
Everything is good until 2018-12-20 and 2018-12-21 the result is duplicated.
For the other users it appeared too.
For example: data2
The code looks like this:
require_once("database.inc.php");
$mysqli = new mysqli($servername, $username, $password, $dbname);
$mysqli->set_charset("utf8");
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
$packer = $_GET['pakowacz'];
$sql = "SELECT aso, number, cast(date_of_scan as date) FROM $packer where DATE(date_of_scan) between '$_GET[date_from]' and '$_GET[date_to]' ";
$sql2 = "SELECT DATE(date_of_scan) AS Data, COUNT(*) AS amount FROM $packer where DATE(date_of_scan) between '$_GET[date_from]' and '$_GET[date_to]' GROUP BY DATE(date_of_scan) ORDER BY Data ";
if ($result2 = mysqli_query($mysqli, $sql2)) {
mysqli_free_result($result2);
} else {
echo $mysqli->error.'<br />';
}
if ($result3 = mysqli_query($mysqli, $sql)) {
$rowcount = mysqli_num_rows($result3);
mysqli_free_result($result3);
} else {
echo $mysqli->error.'<br />';
}
$data = date("Y/m/d");
echo 'Report of packer: '.$_GET['pakowacz']."<br />";
echo 'Raport from: '.$_GET['date_from']."<br />";
echo 'Raport to: '.$_GET['date_to'].'<br />';
echo 'Amount of packed: '.$rowcount;
echo '<table class="sortable"><tr><td width="25">DATA</td><td width="110">amount</td><td width="110">bonus</td></tr>';
$result = $mysqli->query($sql2);
if ($result->num_rows > 0) {
$bonuswhole = 0;
while ($row = $result->fetch_assoc()) {
$amount = $row["amount"];
if ($amount < 30) {
$bonus = '0';
} else if(($amount > 30 && $amount <= 40)) {
$bonus = $amount * 0.3;
} else if($amount > 40) {
$bonus = 12+(($amount-40)*0.5);
}
$bonuswhole += $bonus;
echo '<tr>'.'<td width="110">'.$row["Data"].'</td>'.'<td width="110">'.$row["amount"].'</td>'.'<td width="110">'.$bonus.'</td>'.'</tr>';
}
} else {
echo $mysqli->error;
}
echo "</table>";
echo 'Calosc premii od '.$_GET['date_from'].' do '.$_GET['date_to']." :".$bonuswhole.'ST';
$result->free();
require_once('statystics.php');
$mysqli->close();
Did I make mistake? Do you need more info?