我正试图在PHP中创建一个循环,在这个循环中,购物车中每多出一件商品,运费就会得到10%的递增折扣。
这是我想要的结果:
- 数量=1:运输=30
- 数量=2:运输=30+(30 x 0.5)
- 数量=3:运输=45+(30 x 0.4)
- 数量=4:运输=57+(30 x 0.3)
- 数量=5:运输=69+(30 x 0.2)
- 数量=6:运输=78+(30 x 0.1)
- 数量=7:运输=81+(30 x 0.1)
- 数量=8:运输=84+(30 x 0.1)
等等。
我现在不知道把$quantity
变量放在哪里。
我的PHP:
<?php
$quantity = 10;
$shipping = 30;
for( $discount = 0.5; $discount >= 0.1; $discount - 0.1 ) {
$shipping = $shipping + ( $shipping * $discount );
}
echo $shipping;
?>
您应该迭代数量值,而不是折扣。
$shippingDefault = 30;
$discount = 0.5;
for ($i = 2 ; $i <= $quantity; $i++) {
if ($discount < 0.1) {
$discount = 0.1;
}
$shipping += ( $shippingDefault * $discount );
$discount -= 0.1;
}