Creating a Voting Poll With PHP And MySQL Part 2/2
In the first article of this two part series, Mitchell discussed how to create the database for a voting poll system. He also talked about adding and deleting polls. In this, the second and final article in the series, he will show you how to create a PHP script that will actually allow users to vote, remember which users have voted, and display the results of the most recent poll.
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:
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:
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: