PHP
  Home arrow PHP arrow Page 6 - Democracy, The PHP Way
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
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? 
PHP

Democracy, The PHP Way
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 21
    2001-04-16

    Table of Contents:
  • Democracy, The PHP Way
  • The Plan
  • Design View
  • Start Me Up
  • Vote Now, Or Forever Hold Your Peace
  • The Number Game
  • Down Memory Lane
  • Cookie-Cutter Code
  • Adding More...

  • 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


    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.

    More PHP Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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