emma57573 Posted October 14, 2008 Share Posted October 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/128363-solved-sum-is-giving-the-wrong-calculation/ Share on other sites More sharing options...
peranha Posted October 14, 2008 Share Posted October 14, 2008 $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. Link to comment https://forums.phpfreaks.com/topic/128363-solved-sum-is-giving-the-wrong-calculation/#findComment-664920 Share on other sites More sharing options...
sasa Posted October 14, 2008 Share Posted October 14, 2008 change line $feed="select * from feedback WHERE user_id=$userid and score=1 OR score=2"; to $feed="select * from feedback WHERE user_id=$userid and (score=1 OR score=2)"; Link to comment https://forums.phpfreaks.com/topic/128363-solved-sum-is-giving-the-wrong-calculation/#findComment-665081 Share on other sites More sharing options...
emma57573 Posted October 14, 2008 Author Share Posted October 14, 2008 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 $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); Link to comment https://forums.phpfreaks.com/topic/128363-solved-sum-is-giving-the-wrong-calculation/#findComment-665265 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.