werushka Posted January 21, 2009 Share Posted January 21, 2009 $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 https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/ Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 Yes it probably is possible Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741681 Share on other sites More sharing options...
werushka Posted January 21, 2009 Author Share Posted January 21, 2009 with switch statement of something else...I am a noob learning Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741704 Share on other sites More sharing options...
MadTechie Posted January 21, 2009 Share Posted January 21, 2009 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 https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741719 Share on other sites More sharing options...
werushka Posted January 21, 2009 Author Share Posted January 21, 2009 Thanks MadTechie I haven't learned arrays yet but the above is mine after 3 hours of php learning from Php and Mysql Web Development 4th edition Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741776 Share on other sites More sharing options...
Lodius2000 Posted January 21, 2009 Share Posted January 21, 2009 werushka... your sig links to http://"http//www.haberyorumu.com/" Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741779 Share on other sites More sharing options...
dracolytch Posted January 21, 2009 Share Posted January 21, 2009 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 https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-741786 Share on other sites More sharing options...
werushka Posted January 23, 2009 Author Share Posted January 23, 2009 werushka... your sig links to http://"http//www.haberyorumu.com/" Thanks Lodius to point that out Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744212 Share on other sites More sharing options...
werushka Posted January 23, 2009 Author Share Posted January 23, 2009 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 https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744213 Share on other sites More sharing options...
printf Posted January 23, 2009 Share Posted January 23, 2009 Just another way... <?php $allproducts = 22; $discount = $allproducts > 99 ? 15 : $allproducts > 49 ? 10 : $allproducts > 9 ? 5 : 0; echo $discount; ?> Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744275 Share on other sites More sharing options...
xangelo Posted January 23, 2009 Share Posted January 23, 2009 I think your one line solution may confuse them just a bit Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744572 Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 That's short, but there is a point where abbreviating something too much (like that), takes away from legibility of code, making it harder to update in the future, particularly by someone who didn't write it in the first place. Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744578 Share on other sites More sharing options...
genericnumber1 Posted January 23, 2009 Share Posted January 23, 2009 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. Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744584 Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 Six months? I think for me it's more like six days Link to comment https://forums.phpfreaks.com/topic/141684-is-it-possible-to-make-this-code-dry-and-clean/#findComment-744599 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.