Database Articles

  Home arrow Database Articles arrow Page 4 - Adding a Poll to Your Web Site
DATABASE ARTICLES

Adding a Poll to Your Web Site
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 203
    2005-06-29

    Table of Contents:
  • Adding a Poll to Your Web Site
  • Setting the Stage
  • Creating the Form
  • The Complete showvotes.php
  • Expanding the Poll
  • Polling Accuracy

  •  
     

    SEARCH CODEWALKERS

    Adding a Poll to Your Web Site - The Complete showvotes.php


    (Page 4 of 6 )

    Below is the complete code to showvotes.php. The other two files, poll.php and bargraph.php, have already appeared previously in their entirety.

    <?php
    $host 
    'localhost';
    $user 'user';
    $pass 'pass';
    $db 'polls';
    print 
    "&lt;html&gt;\n";
    print 
    "&lt;head&gt;\n";
    print 
    "&lt;title&gt;Vote totals&lt;/title&gt;\n";
    print 
    "&lt;/head&gt;\n";
    print 
    "&lt;body&gt;\n";
    $dbcon mysql_connect($host$user$pass)
      or die(
    'Unable to connect to server ' $host);
    mysql_select_db($db) or die('Unable to find database ' $db);
    if(isset(
    $_POST['vote']) &amp;&ampctype_digit($_POST['vote'])) {
      
    $query 'UPDATE poll_answers SET votes=votes+1
      WHERE choice=' 
    $_POST['vote'];
      
    $result mysql_query($query);
    }
    $num_votes_query 'SELECT SUM(votes) AS sumvotes
    FROM poll_answers'
    ;
    $num_votes_query .= ' WHERE poll_num=1';
    if (
    $result mysql_query($num_votes_query)) {
      
    $row mysql_fetch_array($result);
      
    $sum $row['sumvotes'];
    }
    $totals_query 'SELECT activity, votes FROM poll_answers ';

    if (
    $result mysql_query($totals_query)) {
      print 
    "&lt;table&gt;\n";
      print 
    "&lt;tr&gt;&lt;th&gt;Activity&lt;/th&gt;&lt;th&gt;Votes&lt;/th&gt;&lt;/tr&gt;\n";
      while(
    $row mysql_fetch_array($result)) {
        print 
    "&lt;tr&gt;&lt;td&gt;" $row['activity'] . "&lt;/td&gt;\n";
        print 
    "&lt;td align=\”right\”&gt;" $row['votes'] . "&lt;/td&gt;&lt;/tr&gt;\n";
        if(
    $sum) {
          
    $percent round($row['votes'] * 100 $sum);
          print 
    "&lt;tr&gt;&lt;td&gt;&lt;img src=\”bargraph.php?pct=$percent\”&gt;&lt;/td&gt;\n";
          print 
    "&lt;td align=\”right\”&gt;" $percent "%&lt;/td&gt;&lt;/tr&gt;\n";
        }

      }
      print 
    "&lt;/table&gt;\n";
    }
    print 
    "&lt;/body&gt;\n";
    print 
    "&lt;/html&gt;\n";
    ?>

    More Database Articles Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATABASE ARTICLES ARTICLES

    - MariaDB, a MySQL Alternative, Opens Foundati...
    - Oracle Supporting HP Itanium Again, Finally
    - Completing a Book Inventory Management System
    - Uploading Images for a Book Inventory Manage...
    - Finishing the Add Book Story for a Book Inve...
    - Integration Testing for a Book Inventory Man...
    - User Stories for a Book Inventory Management...
    - Unit Testing a Book Inventory Management Sys...
    - Testing a Book Inventory Management System
    - Implementing Models for a Book Inventory Man...
    - Book Inventory Application: Publishers and B...
    - Handling Publishers in a Book Inventory Mana...
    - Publisher Administration for Book Inventory ...
    - Book Inventory Management
    - Using the SQL Reference Manual

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap