drxm72811 2018-12-31 21:47
浏览 39
已采纳

PHP以奇怪的方式计算

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
", $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?

  • 写回答

1条回答 默认 最新

  • dongshenghe1833 2018-12-31 22:10
    关注

    The problem in the following lines (when $amount>40)

    }else if($amount>40){
    $bonus=12+(($amount-40)*0.5);
    }
    

    You are not just giving 0.5 bonus BUT adding 12 and doing other things ...

    instead you need to replace with

    }else if($amount>40){
    $bonus=$amount * 0.5;
    }
    

    Also your code didn't take in account when $amount = 30

    Good luck

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大