Percentages
2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 23, 2008 (RSS)
By Kenny - October 23, 2008
Ok - it's election time and I can't wait for it to be over. We have a government client website whose sole purpose is to give out election information - especially on the night of the election (giving out up to the minute results). I have configured the program to accept the results from each race.
The first number is early voting which stays the same. Lets say McCain gets 1000 votes and Obama gets 400. Then an hour later we run a report for precincts that have reported so far and McCain gets an additional 400 votes and Obama gets 300. I have set the code to add the early votes to the first report votes. So the website will report McCain 1400 and Obama 700.
When we run the second report it might say McCain 600 and Obama 500. The second report includes numbers from the first report, so we just replace the first report numbers with the second report numbers. The code adds them automatically to the early vote numbers. So the website will now report McCain 1600 and Obama 900.
Now comes the tricky part. Is there an somewhat easy way to turn these numbers into percentages automatically (displaying the vote count and the percentages). So it would show McCain with 1600 / 64% and Obama with 900 / 36%
My initial code is:
The output page can be found at: http://www.johnsoncountyelections.com/results/
The first number is early voting which stays the same. Lets say McCain gets 1000 votes and Obama gets 400. Then an hour later we run a report for precincts that have reported so far and McCain gets an additional 400 votes and Obama gets 300. I have set the code to add the early votes to the first report votes. So the website will report McCain 1400 and Obama 700.
When we run the second report it might say McCain 600 and Obama 500. The second report includes numbers from the first report, so we just replace the first report numbers with the second report numbers. The code adds them automatically to the early vote numbers. So the website will now report McCain 1600 and Obama 900.
Now comes the tricky part. Is there an somewhat easy way to turn these numbers into percentages automatically (displaying the vote count and the percentages). So it would show McCain with 1600 / 64% and Obama with 900 / 36%
My initial code is:
<?php foreach ($election_results_generalRecords as $record): ?>
<table border="0" cellpadding="3" cellspacing="3" width="100%" id="table1">
<tr>
<td colspan="4" align="left" bgcolor="#CDCCBA">
<p align="left"><b><font face="Arial" color="#800000"><?php echo $record['office'] ?></font></b></td>
</tr>
<?php if ($record['candidate_r']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_r'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(REP)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_r'] + $record['votes_r']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_r']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
<?php if ($record['candidate_d']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_d'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(DEM)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_d'] + $record['votes_d']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_d']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
<?php if ($record['candidate_l']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_l'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(LIB)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_l'] + $record['votes_l']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_l']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
<?php if ($record['candidate_g']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_g'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(GRN)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_g'] + $record['votes_g']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_g']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
<?php if ($record['candidate_i']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_i'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(IND)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_i'] + $record['votes_i']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_i']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
<?php if ($record['candidate_wi']): ?>
<tr>
<td width="56%"><font face="Arial" size="2"><?php echo $record['candidate_wi'] ?></font></td>
<td width="7%" align="left"><font face="Arial" size="2">(W-I)</font></td>
<td align="right" width="26%">
<font face="Arial" size="2" color="#000080"><?php echo number_format($record['early_votes_wi'] + $record['votes_wi']) ?></font></td>
<td align="right" width="4%">
<font size="2" face="Arial">
<?php if ($record['winner_wi']): ?><img border="0" src="http://www.johnsoncountyelections.com/images/checkmark.png"><?php endif ?></font></td>
</tr>
<?php endif ?>
</table>
<?php endforeach; ?>
The output page can be found at: http://www.johnsoncountyelections.com/results/
Re: [sagentic] Percentages
By Dave - October 23, 2008
Hi Sagentic,
First off, the website is probably going to get hammered on election day. So you're going to want to look at the max connections for both Mysql and Apache. It might make sense to have a cronjob update an HTML file once a minute and serve that instead of having a dynamic page that's hitting the database. Depending on the hosting plan though there may be hard coded limits. Let me know if you want to talk about that anymore.
For the question of percentages, this sample code should give you an idea how to do it:
<?php
$aVotes = 1600;
$bVotes = 903;
$totalVotes = $aVotes + $bVotes;
$aPercentage = sprintf("%.02f", $aVotes / $totalVotes * 100);
$bPercentage = sprintf("%.02f", $bVotes / $totalVotes * 100);
print "Total Votes: $totalVotes<br/>\n";
print "A Votes: $aVotes ($aPercentage)<br/>\n";
print "B Votes: $bVotes ($bPercentage)<br/>\n";
?>
The only tricky function here is sprintf(). Basically it just formats the number. Use 02f for 2 decimals, 01f for 1, or 0 for none. You can read about it here: http://php.net/sprintf
Hope that helps, let me know if you need any more detail on the code.
First off, the website is probably going to get hammered on election day. So you're going to want to look at the max connections for both Mysql and Apache. It might make sense to have a cronjob update an HTML file once a minute and serve that instead of having a dynamic page that's hitting the database. Depending on the hosting plan though there may be hard coded limits. Let me know if you want to talk about that anymore.
For the question of percentages, this sample code should give you an idea how to do it:
<?php
$aVotes = 1600;
$bVotes = 903;
$totalVotes = $aVotes + $bVotes;
$aPercentage = sprintf("%.02f", $aVotes / $totalVotes * 100);
$bPercentage = sprintf("%.02f", $bVotes / $totalVotes * 100);
print "Total Votes: $totalVotes<br/>\n";
print "A Votes: $aVotes ($aPercentage)<br/>\n";
print "B Votes: $bVotes ($bPercentage)<br/>\n";
?>
The only tricky function here is sprintf(). Basically it just formats the number. Use 02f for 2 decimals, 01f for 1, or 0 for none. You can read about it here: http://php.net/sprintf
Hope that helps, let me know if you need any more detail on the code.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com