我做了一个函数,计算票价+票的百分比,但它还不完全在那里。
function tickets($maxNumberTickets,$standaardPrice,$AmountOfDiscounts,$DiscountEveryNumberTickets,$percentDiscount){
for($i = 1; $i <= $maxNumberTickets; $i++) {
if($i <= $DiscountEveryNumberTickets){
echo '<option value="' . $i . '">' . $i . ' voor €' . ($standaardPrice * $i) . '</option>';
}else{
for($o = 1; $o <= $AmountOfDiscounts; $o++){
if($i >= ($DiscountEveryNumberTickets* $o)){
echo '<option value="' . $i . '">' . $i . ' voor €' . (($standaardPrice * $i) - (($standaardPrice * $i) * ($percentDiscount * $o / 100))) . ' (' . ($percentDiscount * $o) . '% korting)</option>';
}
}
}
}
}函数调用
tickets(15,20,3,5,10);我说的地方
这就是我所拥有的。
它所做的:
<select class="TicketsVip" name="Tickets">
<option value="1">1 voor €20</option>
<option value="2">2 voor €40</option>
<option value="3">3 voor €60</option>
<option value="4">4 voor €80</option>
<option value="5">5 voor €100</option>
<option value="6">6 voor €108 (10% discount)</option>
<option value="7">7 voor €126 (10% discount)</option>
<option value="8">8 voor €144 (10% discount)</option>
<option value="9">9 voor €162 (10% discount)</option>
<option value="10">10 voor €180 (10% discount)</option>
<option value="10">10 voor €160 (20% discount)</option>
<option value="11">11 voor €198 (10% discount)</option>
<option value="11">11 voor €176 (20% discount)</option>
<option value="12">12 voor €216 (10% discount)</option>
<option value="12">12 voor €192 (20% discount)</option>
<option value="13">13 voor €234 (10% discount)</option>
<option value="13">13 voor €208 (20% discount)</option>
<option value="14">14 voor €252 (10% discount)</option>
<option value="14">14 voor €224 (20% discount)</option>
<option value="15">15 voor €270 (10% discount)</option>
<option value="15">15 voor €240 (20% discount)</option>
<option value="15">15 voor €210 (30% discount)</option>
</select>它应该做什么:
<select class="TicketsVip" name="Tickets">
<option value="1">1 voor €20</option>
<option value="2">2 voor €40</option>
<option value="3">3 voor €60</option>
<option value="4">4 voor €80</option>
<option value="5">5 voor €100</option>
<option value="6">6 voor €108 (10% discount)</option>
<option value="7">7 voor €126 (10% discount)</option>
<option value="8">8 voor €144 (10% discount)</option>
<option value="9">9 voor €162 (10% discount)</option>
<option value="10">10 voor €160 (20% discount)</option>
<option value="11">11 voor €176 (20% discount)</option>
<option value="12">12 voor €192 (20% discount)</option>
<option value="13">13 voor €208 (20% discount)</option>
<option value="14">14 voor €224 (20% discount)</option>
<option value="15">15 voor €210 (30% discount)</option>
</select>我试图更改我的代码,但我所做的任何尝试都不起作用。问题是,10%以上的东西我都有10 %和20%的折扣,但只能显示20%的折扣。
有人知道我在这里做错了什么吗?
发布于 2015-06-05 09:35:30
我用一种新的方式重新构建你的功能,至少对我来说更清晰。而且起作用了。
也许可以重构代码以使代码更加可读性,但我将把这一点留给您。
我使用“模数”操作%:http://php.net/language.operators.arithmetic来检查当前的票证迭代次数是否被$DiscountEveryNumberTickets精确除以。
例如:5%5= 0,10 %5= 0,等等。
function tickets($maxNumberTickets, $standaardPrice, $AmountOfDiscounts, $DiscountEveryNumberTickets, $percentDiscount){
$currentDiscount = 0; // our initial discount is 0%
$discountsSoFar = 1; // we start with the initial discount of 0%
for($i = 1; $i <= $maxNumberTickets; $i++) {
if ($i % $DiscountEveryNumberTickets == 0 && $discountsSoFar <= $AmountOfDiscounts)
{
// time to apply new discount
$currentDiscount += $percentDiscount; // increase the discount
if ($currentDiscount > 100) $currentDiscount = 100; // make sure the discount is not more than 100% :)
$discountsSoFar ++; // increase the number of discounts so far
}
//total price minus the discount
$tickets_price = ($standaardPrice * $i) - (($standaardPrice * $i) * ($currentDiscount /100));
// show the option according to the current settings
echo '<option value="' . $i . '">' . $i . ' voor €' . $tickets_price . ($currentDiscount > 0 ? ' ('.$currentDiscount.'% discount)' : '') .'</option>';
}
}https://stackoverflow.com/questions/30662581
复制相似问题