I'm making a polling system, and i'm wondering what I could do to find the percentage of each number of votes for each answer..
If you need more information, just tell me.
percentages
Moderator: General Moderators
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
total up all the votes for the poll .. then take the number of votes for each question and divide by the total votes for ALL the questions. Then multiply by 100.
Code: Select all
<?php
// votes for questions
$q1_votes = 4;
$q2_votes = 5;
$q3_votes = 8;
$tot_votes = $q1_votes + $q2_votes + $q3_votes;
echo "Question 1 has $q1_votes (".($q1_votes/$tot_votes)*100."%)<br>";
echo "Question 2 has $q2_votes (".($q1_votes/$tot_votes)*100."%)<br>";
echo "Question 3 has $q3_votes (".($q1_votes/$tot_votes)*100."%)";
?>Thanks very much... Here's a little modify ^_^protokol wrote:total up all the votes for the poll .. then take the number of votes for each question and divide by the total votes for ALL the questions. Then multiply by 100.
Code: Select all
<?php // votes for questions $q1_votes = 4; $q2_votes = 5; $q3_votes = 8; $tot_votes = $q1_votes + $q2_votes + $q3_votes; echo "Question 1 has $q1_votes (".($q1_votes/$tot_votes)*100."%)<br>"; echo "Question 2 has $q2_votes (".($q1_votes/$tot_votes)*100."%)<br>"; echo "Question 3 has $q3_votes (".($q1_votes/$tot_votes)*100."%)"; ?>
Code: Select all
<?php
// votes for questions
$q1_votes = 4353454;
$q2_votes = 34534535;
$q3_votes = 84543454345;
$q4_votes = 1;
$tot_votes = $q1_votes + $q2_votes + $q3_votes + $q4_votes;
echo "Question 1 has $q1_votes (".number_format((($q1_votes/$tot_votes)*100),2,'.','')."%)<br>";
echo "Question 2 has $q2_votes (".number_format((($q2_votes/$tot_votes)*100),2,'.','')."%)<br>";
echo "Question 3 has $q3_votes (".number_format((($q3_votes/$tot_votes)*100),2,'.','')."%)<br>";
echo "Question 4 has $q4_votes (".number_format((($q4_votes/$tot_votes)*100),2,'.','')."%)<br>";
?>