|          
续上篇前面我们已经完成了文件设计,并写了surveycode.asp的代码。现在我们来完成
 其它的文件。在完成这些文件的时候,别忘了前面写的程序流程,如果记不清,最好打印
 一份出来对着看。
 
 一、统计结果
 首先我们来完成与surveycode.asp最密切相关的显示统计结果survey_vote.asp
 文件。在上一篇的结尾,我们已经说明了在surveycode.asp中确定的一些参数。
 
 统计结果 survey_vote.asp
 <!--#include file="inc.asp" -->
 <html>
 <head>
 <title>调查统计结果</title>
 <link rel="stylesheet" href="main.css" type="text/css">
 </head>
 <body>
 <%
 '上一句先加入包含文件,引用函数。
 id=request.querystring("id") '获取querystring参数id
 opendb my  '连接数据库
 if id="" then  '如果没有,则不是直接看结果
 id=request.form("id")    '获取form参数id
 if id<>"" then  '如果有值,则是要先统计
 surveycount() '调用统计子程序
 end if
 end if
 if id<>"" then
 disp_survey()  '不管是哪种,最后都显示结果
 end if
 closedb my  '关闭数据库
 
 '-----统计子程序-----
 sub surveycount()
 if session("survey_ok")="" then  '如果还没投票
 no=request.form("res")  '得到答案的编号
 if no<>"" then
 '定义SQL语句,让提交的答案数量+1
 sql="update survey_vote set vote_count=vote_count+1 where vote_no in (" & no &")"
 my.execute sql
 end if
 session("survey_ok")="ok"
 end if
 end sub
 '------------------
 
 '---显示结果子程序---
 sub disp_survey()
 
 '定义SQL语句,得到调查的问题
 sql="select survey_question from survey where survey_id=" & id
 searchtable my,sql,rs '执行查询
 question=rs("survey_question")  '把问题存到question中
 closetable rs  '关闭表
 '定义SQL语句,得到答案的数量总和
 sql="select sum(vote_count) as total from survey_vote where vote_id="& id
 searchtable my,sql,rs
 total=rs("total")
 closetable rs  '关闭表
 
 '定义SQL语句,得到所有的答案文本部份及投票数
 sql="select vote_answer,vote_count from survey_vote where vote_id=" & id
 searchtable my,sql,rs  '执行查询
 '下面用表格来输出统计表
 %>
 <table width="500" border="1" align="center" cellpadding="2" cellspacing="0"
 bordercolorligh="#000000" bordercolordark="#ffffff">
 <tr>
 <td colspan="4" align="center"><b>调查统计结果</b></td>
 </tr>
 <tr>
 <td colspan="4"><b>调查问题:<%=question%></b></td>
 </tr>
 <tr >
 <td width="150" align="center" height="20">答案</td>
 <td width="150" align="center" height="20">投票率</td>
 <td width="100" align="center" height="20">比例</td>
 <td width="100" align="center" height="20">票数</td>
 </tr>
 <%do while not rs.eof
 if total=0 then
 percent=0  '如果没人投票,则百分比为0
 else
 percent=int(rs("vote_count")/total*10000)/100  '计算百分比
 end if
 %>
 <tr>
 <td width="150" align="center"><%=rs("vote_answer")%></td>
 <td width="150" align="left">
 <table border="0" width="<%=percent%>" bgcolor="#CCCC00" height="10">
 <tr>
 <td></td>
 </tr>
 
 </td>
 <td width="100" align="center"><%=percent%>%</td>
 <td width="100" align="center"><%=rs("vote_count")%></td>
 </tr>
 <%
 rs.movenext
 loop
 %>
 <tr>
 <td colspan="4"> 至 <%=now()%> 止,共有 <%=total%> 张投票  
 <a href="javascript:window.close()">关闭窗口</a>
 </td>
 </tr>
 
 <%
 closetable rs  '关闭表
 end sub
 '------------------
 %>
 </body>
 </html>
 
 
 在显示投票过程中,我们用session变量survey_ok来表示是否已经投过票。另外,这显示
 统计中,引用CSS文件来控制表格的样式,你们可以根据自己的要求自己加入。
 
 二、列出所有调查的状态
 现在我们来完成survey.asp,它的主要任务是列出所有的调查状态,包括:
 1、调查的问题,链接到投票表单页面(直接写在本页中);
 2、调查的起启时间;
 3、调查的结束时间;
 4、调查的进行状态:未开始、进行中、已结束;
 5、调查的投票数;
 6、调查的类型,单选还是多选;
 7、另外给出一个链接查看投票结果;
 
 根据这些要求,查询相应的表就可以了,有些语句,比如得到投票总数,SQL语句其实在
 上面的survey_vote.asp中已经写过了。
 
 列出所有调查的状态 survey.asp
 <!--#include file="inc.asp" -->
 <html>
 <head>
 <title>在线调查列表</title>
 <link rel="stylesheet" href="main.css" type="text/css">
 </head>
 <body>
 <%
 id=request.querystring("id") '获取参数
 if id<>"" then  '如果有参数,则显示这个调查表单
 response.write "<SCRIPT Language='JavaScript' SRC='surveycode.asp?id="&id&"'></SCRIPT>"
 else  '否则调用子程序显示状态
 disstat()
 end if
 
 '-----显示状态子程序----
 sub disstat()
 opendb my '连接数据库
 opentable my,"survey",rs '直接打开表
 '下面用表格显示每个记录
 '先显示表头
 %>
 <table width="760" border="1" cellspacing="0" cellpadding="2"
 align="center" bordercolorligh="#000000" bordercolordark="#ffffff">
 <tr>
 <td colspan="8" align="center"><b>在线调查列表</b></td>
 </tr>
 <tr >
 <td width="50" align="center" height="20">编号</td>
 <td width="200" align="center" height="20">调查问题</td>
 <td width="50" align="center" height="20">类型</td>
 <td width="140" align="center" height="20">起启时间</td>
 <td width="140" align="center" height="20">结束时间</td>
 <td width="50" align="center" height="20">状态</td>
 <td width="80" align="center" height="20">已投票数</td>
 <td width="50" align="center" height="20">查看</td>
 </tr>
 <%
 '下面输出每个记录
 do while not rs.eof
 '先读出每个字段
 id=rs("survey_id")
 question=rs("survey_question")
 '读出类型
 if rs("survey_type") then
 stype="多选"
 else
 stype="单选"
 end if
 stime=rs("survey_stime")
 etime=rs("survey_etime")
 '判断状态
 if now()<stime then
 stat="未开始"
 else
 if now<etime then
 stat="进行中"
 else
 stat="已结束"
 end if
 end if
 
 '定义SQL语句,得到答案的数量总和
 sql="select sum(vote_count) as total from survey_vote where vote_id="& id
 searchtable my,sql,tmprs '查询
 total=tmprs("total")
 closetable tmprs  '关闭表
 '下面输出一条记录
 %>
 <tr >
 <td align="center" height="20"><%=id%></td>
 <td height="20">
 <a href="survey.asp?id=<%=id%>"><%=question%></a>
 </td>
 <td align="center" height="20"><%=stype%></td>
 <td align="center" height="20"><%=stime%></td>
 <td align="center" height="20"><%=etime%></td>
 <td align="center" height="20"><%=stat%></td>
 <td align="center" height="20"><%=total%></td>
 <td align="center" height="20">
 <a href="survey_vote.asp?id=<%=id%>" target="_blank">查看</a>
 </td>
 </tr>
 <%
 rs.movenext '移动到下一条,循环
 loop
 %>
 
 <%
 closetable rs  '关闭表
 closedb my '关闭数据库
 end sub
 '----------------------
 %>
 </body>
 </html>
 
 
 到这里,我们已经完成了显示统计结果和列出所有调查的两个文件,最后只剩下
 管理页面了,下一篇我们将来完成管理页面。
 
 |