Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top