Create Coldfusion Poll (Part two - The modules)
|
| |
|
Here you find the first part of the Poll System. |
| |
|
The voting module: |
This module has three main purposed:
-
Check if the user already have voted, if yes send user automatically to show vote results. Else let the user vote.
-
Check if user has selected a question, if not show vote again.
-
After user has voted start the module for new voting results.
|
<!--- Check if cookie is defined: Yes: User have already voted, send user to show results. No: User has not voted, let user vote. ---> <!--- User has voted, so send user to show vote results. ---> <cfif IsDefined("cookie.fusionpoll")> <formaction="cf_poll.cfm?action=dsp_results" NAME="Add_Update" METHOD="post"> <input name="_" type="hidden" value="_"> <script language="JavaScript"> <!-- document.Add_Update.submit(); //-- </script> </form> <!--- End of the </form> that automatically send the visitor to show vote results. ---> </cfif> <!--- </cfif> End of visitor have voted. ---> <!--- Start making tables for vote. ---> <!--- Query table questions and find the poll name. In this example there are only one poll, so QuestionID=1 ---> <cfquery name="Poll_Name" datasource="#request.main_access_DSN#"> SELECT * FROM cf_poll_questions WHERE QuestionID = #QuestionID# </cfquery> <!--- Query the table Answers (same ID) and get all the poll questions. ---> <cfset QuestionID = 1> <cfquery name="Poll_Question" datasource="#request.main_access_DSN#"> SELECT * From cf_poll_answers WHERE QuestionID = #QuestionID# ORDER BY AnswerID ASC </cfquery> <!--- Find numbers of poll questions. ---> <cfset TotalRows = Poll_Question.RecordCount> <!--- Write out our poll. ---> <form action="" method="post" name="Vote_Action"> <table width="<cfoutput>#vote_width#</cfoutput>" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000" bgcolor="#6699CC" id="results" style="border-collapse: collapse"> <tr> <!--- Write the name of the poll. ---> <td bgcolor="#000099"><font color="#FFFFFF"><b><cfoutput>#Poll_Name.Question#</cfoutput></b></font></td> </tr> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0"> <br> <!--- Start a query loop that write out the radio button and question. ---> <cfloop query="Poll_Question" startrow="1" endrow="#TotalRows#"> <tr> <td><input name="AnswerID" type="radio" value="<cfoutput>#AnswerID#</cfoutput>"> <cfoutput>#Answer#</cfoutput><br> <br></td> </tr> </cfloop> <!--- End of the </cfloop> for poll radio buttons and questions. ---> </table></td> </tr> <tr> <td bgcolor="#000099"><!--- (Radio button) Get visitors vote answer. ---> <div align="center"> <input name="vote_answer" type= "button"onClick="changeFormAction('cf_poll.cfm?action=act_results')" value="Vote"> </div></td> </tr> <tr> <td bgcolor="#000099"><!--- Button for view vote results. ---> <div align="center"> <input name="Submit2" type= "button"onClick="changeFormAction('cf_poll.cfm?action=dsp_results')" value="View results"> </div></td> </tr> </table> </form> </cfcase> <!--- End of the </cfcase> vote. ---> | |
| |
| Module: Display vote results. |
|
This module displays the results of the vote.
If the user have not voted a vote button is displayed. |
<!--- Show vote results. ---> <cfcase value="dsp_results"> <!--- Find the poll name. ---> <cfquery name="Find_Poll_Name" datasource="#request.main_access_DSN#"> Select * FROM cf_poll_questions WHERE QuestionID = #QuestionID# </cfquery> <!--- Find number of poll questions. ---> <cfquery name="Number_of_questions" datasource="#request.main_access_DSN#"> SELECT * FROM cf_poll_answers WHERE QuestionID = #QuestionID# </cfquery> <!--- Remember the number of poll questions. ---> <cfset TotalRows = Number_of_questions.RecordCount> <!--- Set the total vote answers like zero. ---> <cfset Tot_answers = 0> <!--- Loop that counts the number of vote answers. ---> <cfloop query="Number_of_questions" startrow="1" endrow="#TotalRows#"> <!--- Find new total vote answers. ---> <cfset Tot_answers = Tot_answers + #Votes#> </cfloop> <!--- End of the </cfloop> that counts number of vote answers. ---> <table width="<cfoutput>#vote_width#</cfoutput>" border="1" align="center" cellpadding="3" cellspacing="0" bordercolor="#000000" bgcolor="#6699CC" id="results" style="border-collapse: collapse"> <!--- Output the poll name. ---> <tr> <td bgcolor="#000099"><font color="#FFFFFF"><b><cfoutput>#Find_Poll_Name.Question#</cfoutput></b></font></td> </tr> <tr> <td><table width="100%" border="0" cellspacing="0" cellpadding="0"> <!--- Loop through all the poll questions. ---> <cfloop query="Number_of_questions" startrow="1" endrow="#TotalRows#"> <tr> <!--- Find the percent of the poll question answer. ---> <td><cfset Percent=Round((Votes / Tot_answers) * 100)> <!--- Output the poll question. ---> <cfoutput>#Answer#</cfoutput>: <br> <!--- Output the poll answer graphical bar. ---> <img align="middle" src="<cfoutput>#request.application.webroot#</cfoutput>/images/<cfoutput>#vote_results_img#</cfoutput>" width="<cfoutput>#Percent#</cfoutput>%" height="10"><br> <!--- Output: number of votes and the vote percents. ---> <cfoutput>#Votes#</cfoutput> votes [<cfoutput>(#Percent#%</cfoutput>] </td> </tr> </cfloop> <!--- End of the </cfloop> through all poll questions. ---> </table></td> </tr> <!--- Output the total numbers of votes. ---> <tr> <td bgcolor="#000099"><font color="#FFFFFF"><b>Total: <cfoutput>#Tot_answers#</cfoutput> Votes.</b></font></td> </tr> <!--- The visitor has not voted, so we make a vote button. ---> <cfif NOT IsDefined("cookie.fusionpoll")> <tr> <formaction="cf_poll.cfm?action=vote" method="post" name="Vote_Action"> <td bgcolor="#000099"><div align="center"> <input name="Submit2" type="submit" value="Vote"> </div></td> </form> </tr> </cfif> <!--- End of </cfif> make vote button. ---> </table> | | |
| |
| Module: New vote results. |
| This module calculates the new vote results. |
<!--- The cookie is defined. Visitor has voted, so send visitor to show vote results. ---> <cfif IsDefined("cookie.fusionpoll")> <formaction="cf_poll.cfm?action=dsp_results" NAME="Add_Update" METHOD="post"> <input name="_" type="hidden" value="_"> <script language="JavaScript"> <!-- document.Add_Update.submit(); //-- </script> </form> <!--- End of the form that send visitor automatically to show results, if visitor have voted. ---> </cfif> <!--- End </cfif> for visitor that has voted. ---> <cfif IsDefined("Form.AnswerID")> <!--- cftransaction:Groups multiple queries into a single unit. The cftransaction tag provides commit and rollback processing. ---> <cftransaction> <!--- Find the question visitor has voted. ---> <cfquery name="Last_vote" datasource="#request.main_access_DSN#"> SELECT * FROM cf_poll_answers WHERE AnswerID = #Form.AnswerID# </cfquery> <!--- Add visitors vote to the number of votes for this question. ---> <cfset New_Votes=Last_vote.Votes + 1> <!--- Update our database with the new number. ---> <cfquery name="Update_Vote" datasource="#request.main_access_DSN#"> UPDATE cf_poll_answers SET Votes=#New_Votes# WHERE AnswerID=#AnswerID# </cfquery> </cftransaction> <!--- Set the cookie. ---> <!--- Comment out 'set cookie' when you are testing. ---> <cfcookie name="fusionpoll" value="1" expires="never"> ---> <!--- Ok, user has voted. Send user automatically to show vote results. ---> <cflocationurl="cf_poll.cfm?action=dsp_results"> <cfelse> <!--- If user has not selected a radio button, send user back to vote. ---> <cflocation url = "cf_poll.cfm?action=vote"> </cfif> | |
| |
Advertisement

No User Comments.
No User Comments, be the first one to write your comments?