Jump to content

[SOLVED] Work out a percentage ... but within min or max range


jimbob-2k7

Recommended Posts

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) £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

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 :)

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.