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

About This Tutorial
Author: Alex Allen-Turl
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF5
Total Views: 38,142
Submission Date: December 21, 2002
Last Update Date: June 05, 2009
All Tutorials By This Autor: 7
Discuss This Tutorial
  • 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.

  • 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????

Advertisement

Sponsored By...
Powered By...