PHP/Scripting polling

SpaceshipSpaceship
Watch

nick-8318

Established Member
Impact
23
You know those popular polls on all these hip hop websites, right? What do the professional scripters do about making sure the results add up to 100%?

Example 1:
Question 1: 3 votes, 33%
Question 2: 3 votes, 33%
Question 3: 3 votes, 33%
Total % = 99%

Example 2:
Question 1: 50 votes, 50%
Question 2: 30 votes, 30%
Question 3: 20 votes, 20%
Total % = 100%

What do they do to make sure they add up to 100? I don't wan't to add/subtract a percentage point to something to make it equal 100. Do I just leave it as 99 or what?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
What you would do is divide each result by the total number of votes. Here's a basic example to give you an idea of how you could do it with PHP.

Code:
<?php

// Calculating Poll Results Example

// Imagine each option has a certain number of votes.
$option1 = 20;
$option2 = 30;
$option3 = 87;

$totalvotes = $option1+$option2+$option3;

$percent1 = round($option1 / $totalvotes * 100, 2);
$percent2 = round($option2 / $totalvotes * 100, 2);
$percent3 = round($option3 / $totalvotes * 100, 2);

?>
Results:<br />
Option 1: <?=$percent1?>%<br />
Option 2: <?=$percent2?>%<br />
Option 3: <?=$percent3?>%<br />

Total votes:<br />
<?=$totalvotes?>
 
0
•••
Appraise.net

We're social

Escrow.com
Spaceship
Rexus Domain
CryptoExchange.com
Domain Recover
CatchDoms
DomDB
NameFit
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back