I wonder if someone could please help me out a little. I have a loop that loops through the contents of a shopping cart. I wish to apply a 25% discount to additional items purchased. So basically 1st item is full price and every other item is reduced by 25%. I've tried various methods but all i seem to get is the discount apply to all or nothing.
The loop below works perfectly if i remove the if
statement and its contents thus not wishing to apply a discount. As it currently stands it does not add a discount at all. If i remove the if
condition and use it's contents then it will apply a 25% discount to all items.
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
$Total += $ShoppingCart[$Loop][Price];
if($Loop > 1) {
$Total += $ShoppingCart[$Loop][Price];
$PercentageAmount = 25;
$TotalPrice = $TotalPrice * ((100-$PercentageAmount) / 100);
}
}
Edited:
Unfortunately none of the answers, although maybe technically good, do not fix my problem. I have had to result to placing 2 if statements within a loop and then calculating their combined total. Not an ideal solution but works perfectly never the less. Somehow i need the sort it so then the most expensive item is at full price. It would be much easier if i was not tied to using a loop in this fashion and instead could use array functions.
$i = 0;
for($Loop = 0; $Loop < count($Cart); $Loop++)
{
if($i == 0) {
$Total += $ShoppingCart[$Loop][Price];
}
if($i > 0) {
$TotalMulti += $ShoppingCart[$Loop][Price];
$TotalMulti = $TotalMulti * .75;
}
$i++;
}
$NewTotal = $Total + $TotalMulti;