Jump to content

[SOLVED] Sum is giving the wrong calculation?


emma57573

Recommended Posts

Whay is this giving me the wrong calculation? I am trying to work out the percentage of positive feedback this should be giving me 50% but is outputting at 40%

 

 

The structure of feedback is like this:

 

user_id,score,Comment,from_id,id,get_date_proid

 

The important fields here are user_id and score

 

Score works like this:

1= positive

2= negative

3= neutral

 




$feed="select * from feedback WHERE user_id=$userid and score=1 OR score=2";
//echo $freetplquery;
$sum=mysql_query($feed);
$feedpercent=mysql_fetch_array($sum);
    

$num_rows = mysql_num_rows($sum); 



$feedback="select * from feedback WHERE user_id=$userid and score=1";
//echo $freetplquery;
$possum=mysql_query($feedback);
$feedscore=mysql_fetch_array($possum);
    

$num_pos = mysql_num_rows($possum); 


$score+= (($num_pos / $num_rows) * 100);

 

 

 

Im guessing its my sum thats incorrect but am unsure

 

 

$feedback="select * from feedback WHERE user_id=$userid and score=1";

 

should be

 

$feedback="select COUNT(*) from feedback WHERE user_id=$userid and score=1";

 

 

 

$feed="select * from feedback WHERE user_id=$userid and score=1 OR score=2";

 

should be

 

$feed="select COUNT(*) from feedback WHERE user_id=$userid and score=1 OR score=2";

 

That may help.

Thanks It still wasnt calculating right so in the end i took out the 'OR' part and added the two together instead, it seems to be giving me the right percentage at last  ;D

						   
$feed="SELECT count(*) from feedback WHERE user_id=$user_id and score=2";
$sum=mysql_query($feed);
$feedpercent=mysql_fetch_array($sum);
$num_rows = mysql_num_rows($sum); 



$feedback="SELECT count(*) from feedback WHERE user_id=$user_id and score=1";
$possum=mysql_query($feedback);
$feedscore=mysql_fetch_array($possum);   
$num_pos = mysql_num_rows($possum); 

$addsum+= $num_rows + $num_pos;


$score+= (($num_pos / $addsum) * 100); 

 

 

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.