Home arrow MySQL arrow Page 3 - Creating a Voting Poll With PHP And MySQL Part 2/2

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.

Author Info:
By: Mitchell Harper
Rating: 4 stars4 stars4 stars4 stars4 stars / 212
January 16, 2002
  1. · Creating a Voting Poll With PHP And MySQL Part 2/2
  2. · Capturing votes for the poll
  3. · Displaying the tallied results
  4. · Conclusion

print this article

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))




case $pRow["answer1"]:



case $pRow["answer2"]:



case $pRow["answer3"]:



case $pRow["answer4"]:



case $pRow["answer5"]:





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:


for($i = 1; $i <= 5; $i++)


if($pRow["answer$i"] != "")




<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 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 bgcolor="#FFEFCE" style="padding-left:10" width="5%" colspan="1" height="21">








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:

Displaying the polls voting results
blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials