Jump to content

Is it possible to make this code dry and clean


werushka

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.