jimbob-2k7 Posted February 5, 2008 Share Posted February 5, 2008 Hi All, What I am trying to do is this: 1) We start off with a price, we will say £40. 2) Then work out what 10% of the price ... so that will be £4. 3) That value then must be between a min (£3) and max (£6) value. So £4 will be fine because it's between £3 and £6. But if we start off with £80 (then 10%, so it will be £ £8 is greater than the max vaule, so set it to the max value. I hope you guys get what I am trying to do. Cheers, Jim Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/ Share on other sites More sharing options...
PHP Monkeh Posted February 5, 2008 Share Posted February 5, 2008 <?php $min = 3; $max = 6; $price = 80; $newPrice = $price/10; // Here's your percentage if($newPrice > $max) { $max = $newPrice; } ?> No need to check whether it falls within the range is there? Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458621 Share on other sites More sharing options...
trq Posted February 5, 2008 Share Posted February 5, 2008 Its pretty simple math. <?php $min = 3; $max = 6; $price = 40; $result = $price/10; if ($result < $min || $result > $max) { $result = $max; } echo $result; ?> Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458623 Share on other sites More sharing options...
pdkv2 Posted February 5, 2008 Share Posted February 5, 2008 did you mean the board should write the script according to your requirement specifications ? it all about the if and elase Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458624 Share on other sites More sharing options...
jimbob-2k7 Posted February 5, 2008 Author Share Posted February 5, 2008 Wow thanks for the quick reply. What do we do about the lower end ... so anything lower then the £3 will be £3. Its nice to see peoples script and then learn from it Cheers. Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458634 Share on other sites More sharing options...
PHP Monkeh Posted February 5, 2008 Share Posted February 5, 2008 You want anything lower than £3 to be set to £3? If you use my code it'll stay as £3... if what you meant was: If it's lower than the min price, set the min price to the new value, do this: <?php $min = 3; $max = 6; $price = 80; $newPrice = $price/10; // Here's your percentage if($newPrice > $max) { $max = $newPrice; } else if($newPrice < $min) { $min = $newPrice; } ?> Voila Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458643 Share on other sites More sharing options...
jimbob-2k7 Posted February 5, 2008 Author Share Posted February 5, 2008 YAY! that worked thanks alot Link to comment https://forums.phpfreaks.com/topic/89537-solved-work-out-a-percentage-but-within-min-or-max-range/#findComment-458647 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.