MySQL
  Home arrow MySQL arrow Page 3 - Creating a Voting Poll With PHP And MySQL ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MYSQL

Creating a Voting Poll With PHP And MySQL Part 2/2
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 148
    2002-01-16

    Table of Contents:
  • Creating a Voting Poll With PHP And MySQL Part 2/2
  • Capturing votes for the poll
  • Displaying the tallied results
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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:

    Displaying the polls voting results

    More MySQL Articles
    More By Mitchell Harper


     

    MYSQL ARTICLES

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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT