Percentages

2 posts by 2 authors in: Forums > CMS Builder
Last Post: October 23, 2008   (RSS)

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.
Dave Edis - Senior Developer
interactivetools.com