每次购买10张门票,PHP购买门票2欧元

时间:2018-05-10 13:29:03

标签: php

我对PHP的了解尚未达到一定程度,每购买10张门票,门票就会回收2欧元折扣。我只能用10欧元来折扣2欧元,但我怎么能这样做呢?每10张票就提高2欧元?

<h1>Aantal gekochte tickets</h1>
<br><br>
<form method="POST">
Aantal tickets: <input name="tickets"></input><br><br>
<input type="submit" name="submit"></input>
</form>

<?php

if(isset($_POST['tickets'])) {

$tickets = $_POST['tickets'];

echo "<br><br>1 Ticket kost 2 euro<br><br>";

$sum = $tickets * 2;

if ($tickets % 10 == 0) {


    $sum = $tickets *2 - 2;

    echo "Je betaald 2 euro minder bij de 10e ticket<br><br>";

}

echo "Totaal kosten de tickets: " . $sum . "euro";
}

?>

2 个答案:

答案 0 :(得分:0)

这与数学有关,而不是与php有关。

只需将门票数量除以10,然后使用round来舍入您刚才获得的数字。现在,你有10次符合所选票数的数量。

乘以2得到总折扣。

答案 1 :(得分:0)

<h1>Aantal gekochte tickets</h1>
<br><br>
<form method="POST">
Aantal tickets: <input name="tickets"></input><br><br>
<input type="submit" name="submit"></input>
</form>

<?php

if(isset($_POST['tickets'])) {

$tickets = $_POST['tickets'];

echo "<br><br>1 Ticket kost 2 euro<br><br>";

$sum = $tickets * 2;
// if tickets more than 10, it go into condition
if ($tickets >= 10) {

    $discount=$tickets/10; // discount for each 10 tickets
    $sum = $tickets *2 - (floor($discount)*2);

    echo "Je betaald 2 euro minder bij de 10e ticket<br><br>";

}

echo "Totaal kosten de tickets: " . $sum . "euro";
}
?>