Jump to content

Help Using ELSEIF statement to apply a discount to a given quantity


ajenkins198621

Recommended Posts

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>";

...

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.