Jump to content

Recommended Posts

$allproducts = $tireqty + $oilqty + $sparkqty;
				if ($allproducts < 10) {
					$discount = 0;	
				} elseif (($allproducts >= 10) && ($allproducts <= 49)) {
					$discount = 5;
				} elseif (($allproducts >= 50) && ($allproducts <= 99)) {
					$discount = 10;
				} elseif ($allproducts >= 100) {
					$discount = 15;
				}

thanks in advance

Well their is nothing wrong with the if statement but if you was planning to extend it you could use an array or something instead

ie

<?php
$discounts = array(
    100=>15, 
    10=>5,
    50=>10 
    );
asort($discounts);

$allproducts = $tireqty + $oilqty + $sparkqty;

foreach($discounts as $Q => $D)
{
    if($allproducts>=$Q)
    {
        $discount = $D;
        continue;
    }
}
echo $discount;
?>

 

So if you wanted to add 20% discount for 500 or over you only have to add 500=>20 to the array

ie

$discounts = array(
    500=>20, 
    100=>15, 
    10=>5,
    50=>10 
    );

 

But as i said it is fine as it is

In a case like this, you can simplify by cascading the upper bound...

 

$allproducts = $tireqty + $oilqty + $sparkqty;
$discount = 0;
if ($allproducts >= 10) $discount = 5;
if ($allproducts >= 50) $discount = 10;
if ($allproducts >= 100) $discount = 15;

 

At under 10 items, no rules would trigger, discount stays at 0;

At anything over 10 items, the first rule would trigger, giving a discount of 5;

At 50 or more items, the first two rules would trigger, giving a discount of 10;

And so on...

 

There's a zillion ways to code something like this, depending if you want simple or flexible. The above is the simplest I could think of.

 

~D

In a case like this, you can simplify by cascading the upper bound...

 

$allproducts = $tireqty + $oilqty + $sparkqty;
$discount = 0;
if ($allproducts >= 10) $discount = 5;
if ($allproducts >= 50) $discount = 10;
if ($allproducts >= 100) $discount = 15;

 

At under 10 items, no rules would trigger, discount stays at 0;

At anything over 10 items, the first rule would trigger, giving a discount of 5;

At 50 or more items, the first two rules would trigger, giving a discount of 10;

And so on...

 

There's a zillion ways to code something like this, depending if you want simple or flexible. The above is the simplest I could think of.

 

~D

Thanks dracolytch I see where things are going => flexibility

Indeed.

Eagleson's Law of Programming: Any code of your own that you haven't looked at for six or more months, might as well have been written by someone else.

 

If you can't show a one-liner to your buddy and have him tell you what it does, you'll be just as confused as he is later down the road.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.