Creating a Voting System

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



All ColdFusion Tutorials By Author: Alex Allen-Turl
  • 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.
    Author: Alex Allen-Turl
    Views: 15,739
    Posted Date: Tuesday, December 17, 2002
  • Creating a Voting System
    This is a very quick tutorial on how to create a voting system for page relavances and other such things from a scale of 1 to 5!
    Author: Alex Allen-Turl
    Views: 16,545
    Posted Date: Saturday, December 21, 2002
  • Creating Databases in MS SQL2000
    How to get your Data into MS SQL2000 and interface with Coldfusion in 34 Easy Steps!
    Author: Alex Allen-Turl
    Views: 18,246
    Posted Date: Sunday, December 15, 2002
  • Creating Dynamic Bar Charts
    A tutorial showing you how you can create a dynamic bar chart!
    Author: Alex Allen-Turl
    Views: 16,906
    Posted Date: Tuesday, 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!
    Author: Alex Allen-Turl
    Views: 26,824
    Posted Date: Tuesday, December 24, 2002
  • Creating Relationships with ColdFusion
    This tutorial shows how you create Relationships between tables to speed up queries and decrease the overall size of the database
    Author: Alex Allen-Turl
    Views: 12,081
    Posted Date: Friday, December 13, 2002
  • Extensionless Coldfusion
    Ever wanted to be like google and run your scripts without an extension? This tutorial shows you how with Apache or IIS!
    Author: Alex Allen-Turl
    Views: 7,989
    Posted Date: Thursday, 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!!
    Author: Alex Allen-Turl
    Views: 18,323
    Posted Date: Tuesday, December 24, 2002