Maths advice please-deducting first set of values, then more

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Maths advice please-deducting first set of values, then more

Post by simonmlewis »

Ok, we are doing a valuation thing.

So you have a price of £100.00.
At the first stage the reduction might be 50%.

Then the second stage takes that value and deducts a further 20%.

Example: 100.00 - 50%. 50.00.
50.00 - 20% = 10.00.

I cannot seem to work out the best way to do this.
Here it is so far.

Code: Select all

$result = mysql_query ("SELECT * FROM products WHERE id = '$id'");
          while ($row = mysql_fetch_object($result))
          {
          $value = $row->rrp;
          $rrp = $row->rrp;
          } mysql_free_result($result);
          
          if ($age == "0-1") { $value = $value / 100 * 50;
          $value = $rrp - $value;}
          if ($age == "1-2") { $value = $value / 100 * 55;
          $value = $rrp - $value;}
          if ($age == "2-3") { $value = $value / 100 * 60;
          $value = $rrp - $value;}
          if ($age == "3-4") { $value = $value / 100 * 65;
          $value = $rrp - $value;}
          if ($age == "4+") { $value = $value / 100 * 70;
          $value = $rrp - $value;}
 }        
echo "Age: $value";
if ($condition == "good") 
{
  $value = $value / 100 * 20;
  echo "$value";
}
echo "Conditon: $value";
}
Any and all suggestions gratefully received.

Simon :dubious:
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Maths advice please-deducting first set of values, then

Post by social_experiment »

You could swop out the if statements for a switch statement
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

Ok I like that, but how would I use a Switch statement to do mathematics with percentages?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Maths advice please-deducting first set of values, then

Post by social_experiment »

I would guess in the same way you are doing them now; the switch statement would only clean up the if statements not make any changes to the equations themselves.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

Ok. So is there no simpler way taking off a percentage? I did try $value - 50%; but it got upset!!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Maths advice please-deducting first set of values, then

Post by requinix »

simonmlewis wrote:50.00 - 20% = 10.00.
Uh, what?

Code: Select all

100.0 * (1 - 0.5) = 50.0
50.0 * (1 - 0.2) = 40.0
Note the "1 minus" part - you're missing that, and without it you're actually doing a 50% (okay it's the same amount, coincidence) and 80% reduction. Incidentally, that's what you 50-20% example is doing so now I'm not even sure which one you're trying to do.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

Sorry I don't understand that (1 - 0.5)?
If I wanted to reduce a value by 50%, would I do this?
$value = $value x (1 - 0.5); ??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Maths advice please-deducting first set of values, then

Post by requinix »

simonmlewis wrote:Sorry I don't understand that (1 - 0.5)?
If I wanted to reduce a value by 50%, would I do this?
$value = $value x (1 - 0.5); ??
Yes (but 50% isn't a good example).

If you wanted to reduce the price by 20% then you need to subtract 20%.

Code: Select all

$value - ($value * 0.20)
Or written a different way with the $value factored out,

Code: Select all

$value * (1 - 0.20)
If you just multiply by 20% then you are "keeping" that 20%.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

$value = 100.00;
$value - ($value * 0.50);
echo "$value";

That would produce 50.00 would it?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Maths advice please-deducting first set of values, then

Post by requinix »

No, because you don't assign the result of the subtraction back into $value.

Code: Select all

$value = 100.00;
$value = $value - ($value * 0.50);
echo $value;
would give the result you want.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Maths advice please-deducting first set of values, then

Post by social_experiment »

simonmlewis wrote:Then the second stage takes that value and deducts a further 20%.
the snippet below does what you have in mind; hth

Code: Select all

<?php
$innitial = 70;	
$percentageAry = array(50, 20, 10);
//
echo $innitial . '<br />';
foreach ($percentageAry as $percentage) {
	$new = ($innitial / 100) * $percentage;
	$result = $innitial - $new;
	echo $percentage . '% of ' . $innitial . ' is ' . $new ;
	$innitial = $new;
	echo '<br />';
	}
/* Results
------------
70
50% of 70 is 35
20% of 35 is 7
10% of 7 is 0.7
------------
65
50% of 65 is 32.5
20% of 32.5 is 6.5
10% of 6.5 is 0.65
*/
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

Perfect. How is the last "longer" comment going to be better, as it looks like a lot of effort for the same basic result?

... respectfully!
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Maths advice please-deducting first set of values, then

Post by social_experiment »

simonmlewis wrote:How is the last "longer" comment going to be better, as it looks like a lot of effort for the same basic result?
could you explain which comment you are refering to here?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Maths advice please-deducting first set of values, then

Post by simonmlewis »

Yours.
As I said, the last one, which was the one before mine.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Maths advice please-deducting first set of values, then

Post by social_experiment »

i see, if you have a different approach that's much quicker then go with it, the snippet i wrote is how i'd approach the issue :)
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply