Creating a Voting Poll With PHP And MySQL Part 2/2 - Displaying the tallied results
(Page 3 of 4 )
Once a user has cast their vote, they are automatically redirected to the ShowVotes function. The ShowVotes function displays a HTML table with both a numerical and graphical representation of the aggregated votes for the most recent poll.
The ShowVotes function uses an inner join SQL query to retrieve both the details of our poll, and its accumulated answers in one query:
$strQuery = "SELECT * FROM pollQuestions pq ";
$strQuery .= "INNER JOIN pollAnswers pa ON pq.pk_Id = pa.pollId ";
$strQuery .= "WHERE pq.pk_Id = $recentPollId";The table resulting from this command would contain the row of details for the poll from the pollQuestions table. It would also contain each vote whose pollId value matched the pk_Id field in the pollQuestions table.
The votes for each answer will be tallied into a multi-dimensional array named $answertally. Each index in the array has its value initialized to zero:
$answertally[0] = 0;
$answertally[1] = 0;
$answertally[2] = 0;
$answertally[3] = 0;
$answertally[4] = 0;We also initialize a variable named $answertotal which will keep track of the number of votes tallied so far:
$answertotal = 0;Next, we use a while loop to loop through each vote, tallying them up as we go:
while($pRow = mysql_fetch_array($pResult))
{
switch($pRow["answer"])
{
case $pRow["answer1"]:
$answertally[0]++;
break;
case $pRow["answer2"]:
$answertally[1]++;
break;
case $pRow["answer3"]:
$answertally[2]++;
break;
case $pRow["answer4"]:
$answertally[3]++;
break;
case $pRow["answer5"]:
$answertally[4]++;
break;
}
}We get the total number of votes using a for loop:
// Get the total number of votes
for($i = 0; $i < sizeof($answertally); $i++)
$answertotal += $answertally[$i];Now that we have the total number of votes, we use the mysql_data_seek function to move back to the first record in the result set:
@mysql_data_seek($pResult, 0);We move back to the beginning of the result set so that we can loop through them and display their names and percentages of votes in a HTML table. We use the following for loop to do so:
<?php
for($i = 1; $i <= 5; $i++)
{
if($pRow["answer$i"] != "")
{
?>
<tr>
<td bgcolor="#FFEFCE" style="padding-left:10" width="50%" colspan="1" height="21">
<?php echo $pRow["answer$i"] . " [" . number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", '') . "%]" . "<br>"; ?>
</td>
<td bgcolor="#FFEFCE" style="padding-left:10" width="45%" colspan="1" height="21">
<img src="line.gif" width="<?php echo ($answertally[$i-1] / $answertotal) * 100; ?>" height="5">
</td>
<td bgcolor="#FFEFCE" style="padding-left:10" width="5%" colspan="1" height="21">
</td>
</tr>
<?php
}
}
?>The trick to displaying poll results graphically is to use an image and stretch it to physically represent the tally of each option, using its width tag as a percentage. The ShowVotes function gets the percentage for each vote using this line of code:
<?php echo ($answertally[$i-1] / $answertotal) * 100; ?>This just simply divides the number of votes per answer by the total number of votes and multiplies it by 100. So if we had 6 votes for soccer out of a total of 53 votes, then the percentage would be 11.320754...
We use PHP's number_format function to round the resultant percentage to zero significant decimal places, like this:
number_format(($answertally[$i-1] / $answertotal) * 100, 0, ".", '')The voting results for a poll that I created using the managepoll.php page looks like this:

Next: Conclusion >>
More MySQL Articles
More By Mitchell Harper