Jump to content

Another math problem


timmah1

Recommended Posts

ok, I have three items in the database

 

group01

group02

totalgroup

 

group01 must total totalgroup

group02 must total totalgroup

 

What i'm trying to do is make a percentage from 0-100%, but both groups must equal the totalgroup for it to be 100%

 

So, if totalgroup equals 100 than group01 and group02 must each equal 100

 

Then I take group01 and group02, add them together, then divide by totalgroup to give me a percentage of 0%-100%.

 

The problem I'm having is if totalgroup equals 200, and group01 equals 200 and group02 equals 0, it still shows 100%, and it shouldn't because group02 must equal totalgroup.

Does this make sense?

 

The code I'm using now is this

if($row1['group01] = $totalgroup) {
$totaloverall= $row1['group01'];
}
else {
$totaloverall = $row1['group01] / $totalgroup;
}
if($row1['group02] = $totalgroup) {
$totaloverall01= $row1['group02'];
}
else {
$totaloverall01 = $row1['group02] / $totalgroup;
}

 

Then this

$total = $totaloverall+$totaloverall01;

$number =  round(($total / $totalgroup) * 100) ;
if($number < 0 ) $number = 0 ;
if($number > 100) $number = 100 ;

 

 

Can anybody help me out here?

 

Thanks in advance

Link to comment
Share on other sites

Yes, that works, and I've tried that, but happens is

 

if totalgroup = 200 and group01 = 200 and group02 =0, the total is still 100%, and it shouldn't be because group02 has to equal totalgroup as well

 

$totalnum=200;

$group1=200;

$group2=0;

$grouptotal=group1+group2;

$grouptotal=$grouptotal/2;

$grouptotal=$grouptotal/$totalnum;

$grouptotal=$grouptotal*100;

 

That would be 50%. Of course I think if group1 = 100, group2=200, and totalnum = 300 it would be 100% and thats not what you want.

 

Maybe like:

 

$group1=100;
$group2=300;
$totalnum=400;
$totalnumdiv=$totalnum/2;

if ($group1==$totalnumdiv && $group2==$totalnumdiv) {
  echo("Group1 and group2 are the same number and equal to totalnum.");
}
else {
  echo("Group1 and group2 are not the same number but may be equal to totalnum."); //This is what it should return.
}

 

I think that should work. Try it with group1 and group2 equaling 200.

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.