;中国 COLD FUSION 用户组; WWW.CFWINDOW.COM 
您的位置 :首页 >> CF 技巧文章 >> 阅读文章内容 [ 关闭窗口 ]      

技巧文章内容 
    投票站的例子
文章作者 : CFANS [ agazhang@eastday.com ]          Web URL : http://www.lovease.com
上载日期 : 2001-01-06

在数据库里分别制作两个表Answer和Question。如下:
ANSWER表
AnswerID 自动编号 主键
QuestionID 数字(问题编号)
Answer 文本255(答案)
Votes 数字
OrderNum 文本50

QUESTION表
QuestionID 自动编号 主键 (问题编号)
Question 文本255(问题内容)
QuestionDateTime 日期/时间(发布时间)
IsActive 是/否

并分析以下两个文件CFQuickPoll.cfm 和CFQuickPollResults.cfm

<!--- CFQuickPoll.cfm --->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD>
<TITLE>Cold Fusion - Quick Poll</TITLE>
</HEAD>

<BODY BGCOLOR = "#ffffff">

<!--- Query all the answer for the question. --->
<!--- In this case, the question id is hardcoded. --->
<CFQUERY DATASOURCE = "QuickPoll" NAME = "Question">
SELECT Q.QuestionID, Q.Question, A.AnswerID, A.Answer
FROM Question Q INNER JOIN Answer A ON Q.QuestionID = A.QuestionID
WHERE Q.QuestionID = 1
ORDER BY A.OrderNum
</CFQUERY>

<SCRIPT LANGUAGE = "JavaScript">
<!--
// Function to ensure one of the answers was selected when the user clicks 'Vote'
function ProcessForm()
{
// Set the proceed variable
var Proceed = 0;
// Loop through the number of answers to ensure that one is checked.
// JavaScript objects start the count at 0, so the loop is from 0
// to the recordcount - 1
for (i=0; i<=<CFOUTPUT>#Question.RecordCount#</CFOUTPUT>-1; i++)
{
// If the answer is checked, set proceed to 1 and break out of the loop.
if (document.Poll.AnswerID[i].checked)
{
Proceed = 1;
break;
}
}
// If proceed = 0, display a message.  Otherwise process the form.
if (Proceed == 0)
{
alert("You must select an answer to place your vote.  If you wish to just view the results, click on the 'View Results' link.");
}
else
{
document.Poll.submit();
}
}
//-->
</SCRIPT>

<FORM ACTION = "CFQuickPollResults.cfm" METHOD = "post" NAME = "Poll">
<TABLE BORDER = "0" CELLSPACING = "0" CELLPADDING = "5">
<CFOUTPUT QUERY = "Question" GROUP = "QuestionID">
<!--- Pass the question id as a hidden variable. --->
<INPUT TYPE = "hidden" NAME = "QuestionID" VALUE = "#QuestionID#">
<TR BGCOLOR = "##ff9900">
<TD COLSPAN = "2"><B>#Question#</B></TD>
</TR>
<!--- Display the answers and their checkboxes  --->
<CFOUTPUT>
<TR BGCOLOR = "##c0c0c0">
<TD>#Answer#</TD>
<TD><INPUT TYPE = "radio" NAME = "AnswerID" VALUE = "#AnswerID#"></TD>
</TR>
</CFOUTPUT>
</CFOUTPUT>
<TR BGCOLOR = "#c0c0c0">
<TD COLSPAN = "2" ALIGN = "center"><INPUT TYPE = "button" VALUE = "Vote" onClick = "ProcessForm()"></TD>
</TR>
<TR BGCOLOR = "#c0c0c0">
<TD COLSPAN = "2" ALIGN = "center"><A HREF = "CFQuickPollResults.cfm?QuestionID=1">View Results</A></TD>
</TR>
</TABLE>
</FORM>

</BODY>
</HTML>

递交给第二个文件
<!--- CFQuickPollResults.cfm --->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>
<HEAD>
<TITLE>Quick Poll Results</TITLE>
</HEAD>

<BODY BGCOLOR = "#ffffff">

<!--- If AnswerID is defined, query the number of votes for the question/answer combo, --->
<!--- increment it and update the database.  --->
<!--- This will not process if the user clicked 'View Results' --->
<CFIF IsDefined("AnswerID")>
<CFTRANSACTION>
<CFQUERY DATASOURCE = "QuickPoll" NAME = "LastVote">
SELECT Votes
FROM Answer
WHERE QuestionID = #QuestionID#
AND AnswerID = #AnswerID#
</CFQUERY>
<CFSET NewVotes = LastVote.Votes + 1>

<CFQUERY DATASOURCE = "QuickPoll" NAME = "NewVote">
UPDATE Answer
SET Votes = #NewVotes#
WHERE QuestionID = #QuestionID#
AND AnswerID = #AnswerID#
</CFQUERY>
</CFTRANSACTION>
</CFIF>

<!--- Query the question and the total number of votes for all the answers --->
<CFQUERY DATASOURCE = "QuickPoll" NAME = "Totals">
SELECT Q.Question, SUM(A.Votes) AS TotalVotes
FROM Question Q INNER JOIN Answer A ON Q.QuestionID = A.QuestionID
WHERE Q.QuestionID = #QuestionID#
GROUP BY Q.Question
</CFQUERY>

<!--- Query the answers and the number of votes for each one. --->
<CFQUERY DATASOURCE = "QuickPoll" NAME = "Results">
SELECT A.Answer, A.OrderNum, A.Votes
FROM Answer A
WHERE A.QuestionID = #QuestionID#
ORDER BY A.OrderNum
</CFQUERY>

<TABLE BORDER = "1">
<!--- Display the question --->
<CFOUTPUT QUERY = "Totals">
<TR>
<TD COLSPAN = "3"><B>#Question#</B></TD>
</TR>
</CFOUTPUT>
<!--- Display the answers and their number of votes. --->
<CFOUTPUT QUERY = "Results">
<!--- Set the percent of total votes for each answer. --->
<CFSET Percent = Round((Votes / Totals.TotalVotes) * 100)>
<TR>
<TD><B>#Answer#</B></TD>
<!--- Dispaly the percentage of votes in a graph format. --->
<!--- The width of the bar image is equal to the percentage. --->
<TD>
<TABLE BORDER = "0">
<TR>
<TD VALIGN = "middle"><IMG ALIGN = "middle" SRC = "#ImagePath#/OrangeSquare.gif" WIDTH = "#Percent#" HEIGHT = "20"></TD>
<TD VALIGN = "middle"><B>#Percent# %</B></TD>
</TR>
</TABLE>
</TD>
<TD ALIGN = "right"><B>#Votes# votes</B></TD>
</TR>
</CFOUTPUT>
<!--- Display the total number of votes --->
<CFOUTPUT QUERY = "Totals">
<TR>
<TD COLSPAN = "3" ALIGN = "right"><B>Total: #Int(TotalVotes)# votes</B></TD>
</TR>
</CFOUTPUT>
</TABLE>

</BODY>
</HTML>







< 联系我们 --- 中国Cold Fusion用户组>

CFUG 国内(总部):Linkfoxo    上海:CFANS    北京:Cafe,Cyberkid,liwater    沈阳:Wangking
  
哈尔滨:Baiming    浙江:梅盛松    江西:陈末
  
CFUG (国际) Nagoya(名古屋):Codeguru    新加坡:YUZI    新西兰(Auckland):Richard CHEN
Copyright 2000-2001 www.cfwindow.com.All rights reserved

;中国 COLD FUSION 用户组; WWW.CFWINDOW.COM