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

Democracy, The PHP Way
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 22
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek