Server Time:
Monday May 12 2008 09:50 AM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Creating a Voting System
by: Alex Allen-Turl
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

How to create a voting System!
-----------------------------

For my application I've been writing, i decided to make a voting system, and thought I'd share with you all now, how to do it :)


for this i am using my application which is about rather attractive women
http://webdev.webleicester.co.uk/test/women/
(not complete yet)


The Database
------------
  vote_id = (Autonumber) (primary)
  woman_id = (int) (4)
  one = (int) (4)
  two = (int) (4)
  three = (int) (4)
  four = (int) (4)
  five = (int) (4)
  avg_votes = (decimal) (9) (scope=1)
  totalvotes = (int) (4)

Quick not about avg_votes,
this column has to be able to accept decimal places the scope=1 is something you have in MS SQL2000 and the 1 means how many decimals it can use
IF the script doesn't appear to be working open up the DB and type in 4.32432 it will then round up to where its been set to so if it allows no decimal places it'll go to 4 if its scope is set to 2 it'll round to 4.32

But to be sure, always read your documentation

back to the tutorial!


On the main page we have links that look like this

<a href="vote.cfm?id=#id#&vote=1">1</a>
<a href=
"vote.cfm?id=#id#&vote=2">2</a>
<a href=
"vote.cfm?id=#id#&vote=3">3</a>
<a href=
"vote.cfm?id=#id#&vote=4">4</a>
<a href=
"vote.cfm?id=#id#&vote=5">5</a>

Then on the next page we will add that vote under the column that equals url.vote and it will just +1 so then you can be more specific other than averages you could show that 50 people voted 5  where as 120 people voted as 1


then we have this for the voting page

Vote.cfm
--------

<html><head><title>Link Updated!</title></head><body>

<!--- Collect information --->
<cfquery name="GetVotes" Datasource="#application.dsn#"
    SELECT *
    FROM votes
    WHERE woman_id=#url.id#
</cfquery> 


<!--- Set the parameters --->
<CFSET total = #GetVotes.totalvotes#>
<CFSET average = #GetVotes.avg_vote#>
<CFSET voteone = #GetVotes.one#>
<CFSET votetwo = #GetVotes.two#>
<CFSET votethree = #GetVotes.three#>
<CFSET votefour = #GetVotes.four#>
<CFSET votefive = #GetVotes.five#>

<!--- update the columns with the votes --->
<cfif url.vote eq 1>
   <CFSET voteone = voteone + 1>
   <cfquery name=
"UpdateVote" Datasource="#application.dsn#"
   
     UPDATE votes
        SET one=#voteone#
        WHERE woman_id=#url.id#
    </cfquery>
</cfif>

<cfif url.vote eq 2>
    <CFSET votetwo = votetwo + 1>
    <cfquery name=
"UpdateVote" Datasource="#application.dsn#"
        UPDATE votes
        SET two='#votetwo#'
        WHERE woman_id=#url.id#
    </cfquery>
</cfif>


<cfif url.vote eq 3>
    <CFSET votethree = votethree + 1>
    <cfquery name=
"UpdateVote" Datasource="#application.dsn#"
       
UPDATE votes
        SET three='#votethree#'
        WHERE woman_id=#url.id#
    </cfquery>
</cfif>


<cfif url.vote eq 4>
    <CFSET votefour = votefour + 1>
    <cfquery name=
"UpdateVote" Datasource="#application.dsn#"
       
UPDATE votes
        SET four=#votefour#
        WHERE woman_id=#url.id#
    </cfquery>
</cfif>


<cfif url.vote eq 5>
    <CFSET votefive = votefive + 1>
    <cfquery name=
"UpdateVote" Datasource="#application.dsn#"
       
UPDATE votes
        SET five='#votefive#'
        WHERE woman_id=#url.id#
    </cfquery>
</cfif>


<!--- Update the Total Votes --->
<cfset total = total + 1>
<cfquery name="UpdateVote" Datasource="#application.dsn#"
    UPDATE votes
    SET totalvotes=#total#
    WHERE woman_id=#url.id#
</cfquery>

<!--- Update the total average 
        the formula we are using is
            1 * votes
         + 2 * votes
         + 3 * votes
         + 4 * votes
         + 5 * votes
         / totalvotes
--->

<cfset step1 = (1 * #voteone#) + (2 * #votetwo#) + (3 * #votethree#) + (4 * #votefour#) + (5 * #votefive#)>
<cfset average = step1 / #total#>

<cfquery name=
"UpdateVote" Datasource="#application.dsn#"
    UPDATE votes
    SET avg_vote=#average#
    WHERE woman_id=#url.id#
</cfquery>


<CFOUTPUT>
    <html>
      <head>
      <meta http-equiv=refresh content=
"0;URL=pageyoujustcamefrom.cfm">
      </head>
      <center>

        <table width=700>
            <tr>
                <td bgcolor=##000000>

                <font color=##AAAA77 face=verdana,arial size=2>
                If your browser does not support javascript page redirection, click <a href="pageyoujustcamefrom.cfm">here</a>.
                </font>
                </td>
            </tr>
        </table>

    </center>
    </html>

</CFOUTPUT>



Then after that you can say

<cfquery name="GetWomen" datasource="#application.dsn#">
    SELECT * 
    FROM women, votes
    where (women.id = votes.Woman_id) 
    AND id = #url.id#
</cfquery> 

<cfoutput name="GetWomen">
    Average Rating #avg_vote# over #totalvotes# votes!
</cfoutput>


hope this helps you all!!


Date added: Sat. December 21, 2002
Posted by: Alex Allen-Turl | Views: 16496 | Tested Platforms: CF5 | Difficulty: Intermediate
Categories Listed: Full Applications

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Extensionless ColdfusionWinner!
Ever wanted to be like google and run your scripts without an extension? This tutorial shows you how with Apache or IIS! - Date added: Thu. October 27, 2005
The Easiest Way to make a form!
This is by far the most easiest and simplest way to make a form and update it to the database, very low maintenance and very very quick!! - Date added: Tue. December 24, 2002
Creating Dynamic Bar Charts
A tutorial showing you how you can create a dynamic bar chart! - Date added: Tue. December 24, 2002
Creating Dynamic Image Galleries
A tutorial showing you how you can upload one Full sized picture, and have a thumbnail automatically created for you, along with descriptions of the image! - Date added: Tue. December 24, 2002
Creating a Link Management System
This quick tutorial, shows you how to creat a Link system, that will count the amount of hits that your links create. - Date added: Tue. December 17, 2002

Additional Tutorials:
· Creating Databases in MS SQL2000

· Creating Relationships with ColdFusion
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Comments on this tutorial
Read previous comments on this particular tutorial
allowing only 1 vote per IP address???
How could I lock this down to only 1 vote per IP address? Can I serch a table of IP addresses (that I create) and if it is not found then accept the vote and log the users IP address and vote into their specific tables? The question is how do I get the user's IP address????
Posted by: David Pienta
Posted on: 12/14/2004 04:11 PM
Cookie or Session
You can use CFCOOKIE or you can use a SESSION variable to identify the user through a process page. You can set the cookies to identify the computer through a number system. Its pretty simple, it just depends. A session variable can be used for an open/close browser event, while the cookie can last until it expires.
Posted by: Zach
Posted on: 05/08/2005 01:35 PM
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
ColdFusion Hosting by HostMySite

You are 1 of 528 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb