Democracy, The PHP Way - The Number Game (
Page 6 of 9 )
Once the database has been
updated with the vote, it's a good idea to display the current results of the
poll. This involves connecting to the database, using the $id variable to
extract the correct record, calculate the total number of votes, and the
percentage each option has of the total, and displaying this information in a
table.
Here's what all that looks like in PHP:
<?
// query successful,display status message...
if ($result)
{
echo "Thank you for voting. Here are the results so far:<p>";
// ...and tabulated results
// get a complete count of votes in each category
$query = "SELECT question, response1, response2, response3, votes1,
votes2, votes3, date from $table WHERE id = $id";
$result = mysql_db_query($database, $query, $connection) or die ("Could
not execute query: $query. " . mysql_error());
// assign the returned values to variables
list($question, $response1, $response2, $response3, $votes1, $votes2,
$votes3, $date) = mysql_fetch_row($result);
// count the total votes
$total = $votes1 + $votes2 + $votes3;
// calculate each as a percentage of the total, round to two decimals
$perc_votes1 = round(($votes1/$total)*100,2);
$perc_votes2 = round(($votes2/$total)*100,2);
$perc_votes3 = round(($votes3/$total)*100,2);
// print it all in a neat table
echo "<table border=0 cellspacing=0 cellpadding=5>";
echo "<tr><td colspan=3><b>$question</b></td></tr>";
// also display an image graph - more on this later!
echo "<tr><td>$response1</td><td> $votes1 ($perc_votes1%)</td><td
rowspan=4 valign=top><img
src=graph.php?votes1=$votes1&votes2=$votes2&votes3=$votes3
border=0></td></tr>";
echo "<tr><td>$response2</td><td> $votes2 ($perc_votes2%)</td></tr>";
echo "<tr><td>$response3</td><td> $votes3 ($perc_votes3%)</td></tr>";
echo "<tr><td><font size=-2>Posted on " . fixDate($date) .
"</font></td><td><font size=-2>$total total votes</font></td></tr>";
echo "</table><p>";
}
// or error in query - display error message
else
{
echo "<i>Error! Please <a href=start.php>try again</a></i>";
}
?>
You need to be careful when converting the absolute
numbers into percentages - if there aren't any votes yet, you can get some
pretty strange "division by zero" errors. This error is not likely at this stage
- after all, you've just added a vote - but it can crop up at a later stage. As
we progress, you'll see the correction I've used to account for this
situation.
The code snippet above references an image named "graph.php".
If you're familiar with PHP's image generation function, you'll immediately
divine that this is the PHP script used to dynamically generate the bar graph.
I'll be discussing this a little later, so ignore it for the
moment.
Finally, you'll see a reference to a fixDate() function in the
last line of the table. This is a very simple function I wrote to convert the
default MySQL date format into something a little more readable. Here's the
function:
<?
// format the date so that it looks prettier
function fixDate($val)
{
$dateArray = explode("-", $val);
$val = date("j M Y", mktime(0,0,0, $dateArray[1], $dateArray[2],
$dateArray[0]));
return $val;
}
?>
Feed fixDate() a date in the default MySQL format (say,
"2001-03-07") and it will return something much friendlier ("7 Mar
2001").
And here's what the final result page looks like:
This article copyright Melonfire
2001. All rights reserved.