web|投票|投票
三、调查表单的生成与结果分析
作 者 : 仙人掌工作室
PollWriter.asp先检查表单变量,包括调查项目名字PollName以及(可选的)字符串LinkText和LinkURL,LinkText和LinkURL描述了用户提交其投票结果之后显示的URL。接下来脚本创建并执行从指定调查项目的S_表和A_表提取记录集的SQL命令,然后遍历这个记录集格式化单选按钮、复选框以及其它提示文本,最后输出该调查项目的HTML表单。表单中问题的名字为“CHOICE”加问题序号。当这个表单被提交的时,它的ACTION将装载PollMaker.asp(这里的函数saveResponses()从CHOICE表单元素提取投票结果,然后更新数据库,代码略)。表单中的其它隐藏变量用于将PollName、OpType、LinkText、LinkURL传递给PollMaker.asp:
< %
' 从S_表和A_表读取调查项目定义
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "poll"
SQLCommand = "Select " & SName & ".ID, " & _
"IType, NoOpinion, Stem, ALabel, Answer FROM " & SName & "LEFT JOIN " & _
AName & " ON " & SName & ".ID = " & AName & ".ID ORDER BY " & _
SName & ".ID, ALabel"
Set objRS = objConn.Execute(SQLCommand)
' 向调查表单输出各个问题
Do While Not objRS.EOF
Response.Write("< TR>< TD>" & objRS("ID") & ".< TD BGCOLOR=""#ffff00"">" & _
objRS("Stem"))
Response.Write("< TR>< TD>< TD>")
ckNoOpinion = IIf(objRS("NoOpinion") = "Y", "", "Checked")
noMove = False
' 除"允许复选",其它各类问题均需检查是否允许不回答问题
ansName = """CHOICE" & objRS("ID") & """"
Select Case objRS("IType")
Case "1" ' 是/否
If ckNoOpinion = "" Then
Response.Write(" < INPUT TYPE=radio NAME=" & ansName & _
" VALUE="" "" Checked>暂不回答 ")
End If
Response.Write("< INPUT TYPE=radio NAME=" & ansName & _
" VALUE=""A"" " & ckNoOpinion & ">是")
Response.Write("< INPUT TYPE=radio NAME=" & ansName & _
" VALUE=""B"">否")
Case "2" ' 赞同程度
...略...
Case "3" ' 语义区分
...略...
Case "4" ' 多项选择
...略...
Case "5" ' 允许复选
...略...
End Select
If NoMove Then
NoMove = False
ElseIf Not objRS.EOF Then
objRS.MoveNext
End If
Loop
%>
PollResult.asp使用与PollWrite.asp同样的SQL命令从A_表(答案)和S_表(问题)提取记录集,接着从对应的R_表读取用户回答结果,用一个二维数组Counts统计投票结果。缺省时结果分析中不包含“没有回答”,如果要包含,则需给出表单变量ShowNoOp并指定其值为True,请参见图4中显示投票结果的URL。
< %
'Globals
Dim PollName
Dim Counts() ' 用户回答的统计数组
Dim nResponses
Dim N ' 用户回答数量,可能不包含"没有回答"选项
Dim objConn
Dim objRS
nResponses = 0
Call Main
Sub Main
Dim likert
Dim i
Likert = Array("强烈反对","反对", "不确定","同意","完全赞成")
PollName = Request("PollName")
ShowNoOp = IIf(UCase(Request("ShowNoOp"))="TRUE", True, False)
' 从A_表和S_表读取调查项目定义
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.Open "poll"
SName = "[S_" & PollName & "]"
AName = "[A_" & PollName & "]"
If Count() = False Then
Response.Write("< BR>此调查项目没有投票结果")
Exit Sub
End If
SQLCommand = "Select " & SName & ".ID, " & _
"IType, NoOpinion, Stem, ALabel, Answer FROM " & SName & _
"LEFT JOIN " & AName & " ON " & SName & ".ID = " & AName & _
".ID ORDER BY " & SName & ".ID, ALabel"
Set objRS = objConn.Execute(SQLCommand)
Response.Write("< TABLE>")
... 输出表单标题,略 ...
Response.Write("< TR>< TD COLSPAN=5> ")
itemIx = 0 ' 问题序号
Do While Not objRS.EOF
IType = objRS("IType")
NoMove = False
N = IIf(ShowNoOp Or IType=5, nResponses, nResponses-Counts(itemIx, 0))
N = IIf(N=0, 1, N)
Response.Write("< TR>< TH ALIGN=right>" & objRS("ID") & _
".< TH ALIGN=left COLSPAN=4 BGCOLOR=""#ffff00"">" & objRS("Stem"))
Response.Wr