percentages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

percentages

Post by phice »

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.
Image Image
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

lol, huh? need more info :)
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

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."%)";
?>
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

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."%)";
?>
Thanks very much... Here's a little modify ^_^

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>"; 
?>
Image Image
Post Reply