ajenkins198621 Posted April 14, 2009 Share Posted April 14, 2009 Hello, I am trying to use an elseif statement (or whatever would work() to provide a discount depending on how many quantities someone orders. I have tried a million different options, and just can't seem to get it to work. Below is the code I have been trying to work with, but it is not correct. Any ideas? <?PHP $price_discountcoffee=price_totalcoffee*(1-discount); ?> <?PHP ... if ($qty_dark > 0) echo "<tr> <td>Quantity of Dark Roast:</td> <td align=\"center\">$qty_dark</td> <td align=\"center\" font=\"#0fc\"> </td> </tr>"; if ($qty_medium > 0) echo " <tr> <td>Quantity of Medium Roast:</td> <td align=\"center\">$qty_medium</td> <td align=\"center\" font=\"#0fc\"> </td> </tr>"; if ($qty_coffeetotal > 0) { if ($qty_coffee ==1) $discount=0.00; elseif ($qty_coffee >=2 && <=3) $discount=0.10; elseif ($qty_coffee >=4 && <=5) $discount=0.16; elseif ($qty_coffee >=6) $discount=0.20; echo " <tr> <td align =\"right\"><strong>Total Coffee:</strong></td> <td align=\"center\"> </td> <td align=\"center\" font=\"#0fc\">$$price_discountcoffee</td> </tr>"; ... ?> Link to comment https://forums.phpfreaks.com/topic/154136-help-using-elseif-statement-to-apply-a-discount-to-a-given-quantity/ Share on other sites More sharing options...
mrMarcus Posted April 15, 2009 Share Posted April 15, 2009 what's wrong? what's the error? Link to comment https://forums.phpfreaks.com/topic/154136-help-using-elseif-statement-to-apply-a-discount-to-a-given-quantity/#findComment-810260 Share on other sites More sharing options...
eRott Posted April 15, 2009 Share Posted April 15, 2009 Hello, I am trying to use an elseif statement (or whatever would work() to provide a discount depending on how many quantities someone orders. I have tried a million different options, and just can't seem to get it to work. Below is the code I have been trying to work with, but it is not correct. Any ideas? <?PHP $price_discountcoffee=price_totalcoffee*(1-discount); ?> <?PHP ... if ($qty_dark > 0) echo "<tr> <td>Quantity of Dark Roast:</td> <td align=\"center\">$qty_dark</td> <td align=\"center\" font=\"#0fc\"> </td> </tr>"; if ($qty_medium > 0) echo " <tr> <td>Quantity of Medium Roast:</td> <td align=\"center\">$qty_medium</td> <td align=\"center\" font=\"#0fc\"> </td> </tr>"; if ($qty_coffeetotal > 0) { if ($qty_coffee ==1) $discount=0.00; elseif ($qty_coffee >=2 && <=3) $discount=0.10; elseif ($qty_coffee >=4 && <=5) $discount=0.16; elseif ($qty_coffee >=6) $discount=0.20; echo " <tr> <td align =\"right\"><strong>Total Coffee:</strong></td> <td align=\"center\"> </td> <td align=\"center\" font=\"#0fc\">$$price_discountcoffee</td> </tr>"; ... ?> May I be the first to ask, did you write this yourself or just copy and paste from somewhere? How can you perform a calculation before actually defining the values of those variables in the equation? Your math is conflicting and your logic flawed. The first problem I notice is with your equation for calculating the price of the discounted coffee: $price_discountcoffee=price_totalcoffee*(1-discount); It should be: $price_discountcoffee = $price_totalcoffee * (1 - $discount); Secondly, where are you defining the values for $price_totalcoffee, $qty_dark, $qty_medium, $qty_coffeetotal and $qty_coffee? All in all, it should look something like this: <?php if ($qty_dark > 0) { echo '<tr><td>Quantity of Dark Roast:</td><td align="center">'.$qty_dark.'</td>'; echo '<td align="center" font="#0fc"> </td></tr>'; } if ($qty_medium > 0) { echo '<tr><td>Quantity of Medium Roast:</td><td align="center">'.$qty_medium.'</td>'; echo '<td align="center" font="#0fc"> </td></tr>'; } if ($qty_coffeetotal > 0) { if ($qty_coffee == 1) { $discount = 0.00; } elseif ($qty_coffee <= 3) { $discount = 0.10; } elseif ($qty_coffee <= 5) { $discount = 0.16; } elseif ($qty_coffee >= 6) { $discount = 0.20; } } $price_discountcoffee = $price_totalcoffee * (1 - $discount); echo '<tr><td align ="right"><strong>Total Coffee:</strong></td><td align="center"> </td>'; echo '<td align="center" font="#0fc">'.$price_discountcoffee.'</td></tr>'; ?> Take care. Link to comment https://forums.phpfreaks.com/topic/154136-help-using-elseif-statement-to-apply-a-discount-to-a-given-quantity/#findComment-810261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.