开发者

SQL/PHP calculate percent of yes/no votes

开发者 https://www.devze.com 2023-03-06 13:58 出处:网络
This has me stumped I have Googled for ages now and have searched here but have had no joy on what is probably 开发者_运维问答a very simple to do ...I dont know maybe im just searching with the wrong

This has me stumped I have Googled for ages now and have searched here but have had no joy on what is probably 开发者_运维问答a very simple to do ...I dont know maybe im just searching with the wrong search terms i really dont know but heres my question ...

I have a basic voting script click yes vote goes up by 1, click no vote goes up by 1

SQL Table:

  • votes
    • id
    • item
    • yes
    • no

what i want to do is show the percent of wins (yes votes) over loses (no votes) rather then the actual number of votes so on the page rather then it saying

"Wins 400 loses 600"

it will calculate the percent between the yes and no and say:

"Wins 40% loses 60%"

as always thanks in advance for any help and assistance.

Update:

How can i get it to show at the moment i'm gettng "id #8 resource"

$query  = "SELECT ((yes / (yes+no)) * 100) as yesPercent, ((no / (yes+no)) * 100) as noPercent FROM table WHERE id=1";
$result = mysql_query($query);

echo $result;

I assume its this line i got wrong (or probably the whole query *novice at work)

$result = mysql_query($query);

but have tried a few mysql_????() and none seem to work


As I understand it your table would hold

id - item - yes   - no
1  - name - 400  - 600

Then you'd query

SELECT ((yes / (yes+no)) * 100) as yesPercent, ((no / (yes+no)) * 100) as noPercent FROM table WHERE id=1


It is very simple indeed.

You want to use this simple proportion

NumberYes : TotalVotes = Percentage : 100

And you can find percentage using the formula

Percentage = (NumberYes * 100) / TotalVotes

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号