写在前面
关于在线调查大家一定不会陌生吧,给出一个问题和数个答案,让用户
填写,然后把结果保存到数据库,自动进行统计,最后给出个统计的图。这
期的跟我学做就来做一个在线调查系统。
一、功能设计
这么简单的系统也要做功能设计?有的人也许会觉得奇怪,不过话说回来
不管怎么样的系统,先做功能设计总是能对系统有个比较清析的了解。让我们
来看看在线调查的功能吧。基本的功能上面已经说了,就是要给出一个问题和数
个答案,然后统计,最后给出图。在这个基础上,我们可以考虑给一个调查加上
一个时间段(有效期),在这个时间段里调查是有效的,过了这段时间就自动结
束这个调查。另外,我们可以指定一个用户一次只能提交一次答案。如果你要限
制得更多,可以指定一个IP只能提交一次答案,不过,这样可能一个网吧里的人
只能有一个提交了。对于调查里的问题,有些可能是单选题,而有些可能是多选
题。最后来说说统计的图,在统计图里要给出答案、每个答案的投票人数,并要
显示出每个答案投票数所占的比例。一般用横的图就可以了,也比较容易实现,
当然,如果你要改成竖的,也可以。
现在根据上面总结出在线调查的功能如下:
1、数据保存在ACCESS 2000 数据库中;
2、每个用户一次访问可以投票一次
3、给出每个调查的统计情况,用统计图来显示
4、每个调查都有个有效期,过期后自动结束。结束了的调查只能查看结果。
5、管理员可以增加调查,修改调查的答案(增加、修改、删除,修改类型)。
6、对于已经结束的调查,管理员只能删除调查,而不能修改答案。
7、只有一个管理员(单用户)
二、数据库设计
现在来设计数据库,根据功能要求,至少要有三个表,一是管理员表,二是
调查表,三是调查结果表。数据库文件名为survey.mdb 可以改为.asp 如果改
的话,请在ASP程序中作相应的修改。
表一、 管理员表 表名: manage
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
manage_id 自动编号 - 在这里没用到,日后扩展用
manage_username 文本 15 管理员用户名
manage_password 文本 15 管理员密码
-----------------------------------------------------------------
建立manage表后加入一条新记录,填入你的管理员用户名和密码,在这里填入的是xmxoxo
表二、 调查表 表名: survey
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
survey_id 自动编号 - 递增、主键、有索引无重复
survey_question 文本 255 调查问题
survey_type 是否 - 类型,否:单选 是:多选
survey_stime 日期 - 长日期,开始时间
survey_etime 日期 - 长日期,结束时间
-----------------------------------------------------------------
表三、调查结束表 表名:survey_vote
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
vote_no 自动编号 - 递增、主键、有索引无重复
vote_id 长整型 - 有索引有重复,小数位0
vote_answer 文本 100 调查答案
vote_count 长整型 - 投票数
-----------------------------------------------------------------
其中,survey_vote表和survey表的id字段有多对一的关系。并不一定要建立这个关系,
但是建立关系会使思路更明确。
三、包含文件
这里所要用到的函数并不多,主要是对数据库进行操作的,如果要防止输入时的
HTML等代码,直接用server.htmlencode进行处理就可以了,所以不需要一个专门的
函数来处理。我们可以沿用上一篇《跟我学》系列《跟我学做树型菜单》里的包含文件。
共用函数文件,文件名:inc.asp
<%
’*******************************************************************
’通用数据库ASP函数
’*******************************************************************
’数据库常数
databasename="survey.mdb" ’数据库名,如果改名的话,在这里修改就行了
’*******************************************************************
’打开数据库
sub opendb(connect)
set connect=server.CreateObject("ADODB.connection")
connect.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &_
server.MapPath(databasename)
connect.Open strconn
end sub
’*******************************************************************
’关闭数据库
sub closedb(connect)
connect.close
set connect=nothing
end sub
’*******************************************************************
’打开单个表读
sub opentable(connect,tbname,myrs)
set myrs=server.createobject("ADODB.recordset")
rssql="select * from " & tbname
myrs.open rssql,connect,1,1
end sub
’*******************************************************************
’关闭临时表
sub closetable(rs)
rs.close
set rs=nothing
end sub
’*******************************************************************
’查询数据库
sub searchtable(connect,sql,rs)
set rs=server.createobject("ADODB.recordset")
rs.open sql,connect,1,1
end sub
’*******************************************************************
’查询并更改数据库
sub changetable(connect,sql,rs)
set rs=server.createobject("ADODB.recordset")
rs.open sql,connect,1,3
end sub
’*******************************************************************
’显示信息 用于调试
Sub w(msg)
response.write msg
end sub
’*******************************************************************
’程序中断 用于调试
sub userstop()
response.end
end sub
%>
到这里暂告一段落,我们已经完成了调查系统的功能、数据库及函数的设计,下一篇
我们来做文件设计,并逐个完成。再次申明,所有的《跟我学做》系列都仅是给朋友们一个
学习的机会,不欢迎索取完整的程序。对于图片等内容,会适当给出下载地址。为了方便大家
学习,下一篇会尽快发布。
续上篇
上一篇中我们已经分析了在线调查的功能,数据库结构以及所要用到的一些
函数。这一篇的主要任务就是文件的设计。设计出要有多少个文件来构成这个在线
调查,每个文件又分担什么任务。并写出这些文件的主要流程,以方便后面的代码编
写。
一、文件设计
按照传统的思路,我们总是把在线调查设计成三个部份,一是显示调查表;二
是显示调查统计结果;三是后台管理。而在实际应用中,我们会发现,显示调查表
往往是在某个网页中的,显示结果一般也是弹出一个窗口来。后台管理则是通过另一
个链接或者登录表单转向到的。为了更方便地使用在线调查,我们把显示调查表部分
写成一个JS脚本,让需要显示调查表的网页通过引用脚本的方式来自由地调用它。好,
看看文件设计
1、inc.asp 包含文件。主要的函数库
2、surveycode.asp 显示调查表程序。在主网页中使用脚本来调用它。
3、survey.asp 调查表列表程序。列出所有调查的状态。
4、survey_vote.asp 显示调查统计结果程序。带上参数表示调查的问题。
5、survey_manage.asp 管理程序。
在这里,我们略过了页面设计,因为风格设计不是我们所要说的内容,所以关于网
页的风格,布局,CSS 等等,请大家自己设计。
二、文件流程
先写出程序的主要流程,可以比较方便于日后修改和扩展、移植。在这里比较重要的
是要在程序中写哪些子程序,如何安排这些子程序。
1、surveycode.asp 显示调查表单
<%
’获取querystring参数,id 表示调查编号
’判断参数正确性
’判断调查是否在有效期中。
’读取调查问题、类型
’输出调查答案,并生成调查表单
’关闭数据库及表
%>
2、survey.asp 显示所有调查状态
<%
’读取数据库
%>
<html>
’显示所有调查状态,并加入链接
</html>
3、survey_vote.asp 显示统计结果。
这里有两个功能,一是没有提交的显示,二是提交了答案后进行统计,然后显示
结果。如果不带参数,就是第一种方式。也可以分为两个文件来完成。
<%
’获取参数。id 表示调查编号 所有数据来自form
’判断是否有参数,有则先进行统计
’没有则直接显示
’统计子程序
%>
<html>
’显示子程序
</html>
4、survey_manage.asp 管理程序。
管理部份比较复杂,要实现较多的功能。先来列一下管理的功能:
1)管理员登录。只有登录后才能进行管理
2)退出登录。完成管理后安全退出。
对调查的管理有:
3)增加一个调查。同时要增加调查答案
4)修改一个调查。修改内容,时间,类型,调查答案的内容、增加、删除
5)删除一个调查。不能删除正在进行的调查。
针对这些功能,来设计它的流程
<%
’获取参数。action表示动作,分别对应上面的功能。
’根据动作来转向相应的子程序
’登录子程序
’退出登录子程序
’执行增加调查问题子程序
’执行增加调查答案子程序
’执行修改调查子程序 问题和答案一起修改
’执行删除调查问题子程序
’执行删除调查答案子程序
<html>
<%
’判断是否登录,没有则显示登录表单
’根据动作显示相应表单
’显示所有调查子程序
’显示单个调查子程序。问题和答案一起显示
’显示增加调查子程序。
’显示登录表单
%>
</html>
三、代码编写
做好了流程设计后,写代码就比较有条理了。让我们从简单的开始。在编写代码
之前,我们要先在数据库里输入一些记录,以便做测试。先加入一条调查问题,和几个
调查答案,并手工输入一些统计信息。
我们先来写显示调查表单的surveycode.asp 这个文件要在其它页面中被调用,
所以我们写成JS和VBS混用的方式。调用的时候可以把它放在某个表格中,用下面的语句:
<SCRIPT Language="javascript" SRC="surveycode.asp?id=1"></SCRIPT>
按照上面的流程,在显示表单前,先要判断一下调查是否存在,是否在进行中。另外,
在表单中要提交一个隐藏的参数,来表示调查的问题编号(id),答案提交的时候,提交的
是答案的编号vote_no
文件名 surveycode.asp
<!--#include file="inc.asp" -->
<%
id=request.querystring("id")
if id<>"" then ’如果有参数
opendb my ’联接数据库
sql="select * from survey where survey_id="& id ’查询语句
searchtable my,sql,rs ’查询数据库
if not rs.eof then ’如果有这个调查记录
question=rs("survey_question") ’读出问题
surveytype=rs("survey_type") ’读出答案类型
stime=rs("survey_stime") ’读出开始时间
etime=rs("survey_etime") ’读出结束时间
closetable rs ’关闭表
if stime<now() and etime>now() then ’如果调查正在进行中
’下面输出调查表单
’先输出表单和问题,表单提交到survey_vote.asp
%>
document.write("<form action=’survey_vote.asp’ target=’_blank’ method=’post’>");
document.write("<table border=’1’ cellpadding=’2’ cellspacing=0’ bordercolorligh=’#000000’");
document.write(" bordercolordark=’#ffffff’ width=’100%’ align=’center’><tbody>");
document.write("<tr><td colspan=’2’ align=’center’><b><%=server.htmlencode(question)%></b></td></tr>");
<%
sql="select vote_no,vote_answer from survey_vote where vote_id="&id ’查询答案的SQL
searchtable my,sql,rs ’执行查询
if not rs.eof then ’如果有答案,就输出
for i=1 to rs.recordcount
%>
document.write("<tr><td align=’right’><input name=’res’ type=’");
<%
if surveytype then ’判断类型,显示单选或者多选
%>
document.write("checkbox");
<%else%>
document.write("radio");
<%end if ’下面这句输出答案的文字和提交的值(vote_no)%>
document.write("’ value=<%=rs("vote_no")%>></td><td><%=rs("vote_answer")%></td></tr>");
<%
rs.movenext
next
’下面几句输出一个隐藏的参数,传递问题编号(id)
’并用一个JS函数来定义点击查看后的链接
%>
document.write("<tr><td colspan=’2’ align=’center’><input type=’hidden’ name=’id’ value=’<%=id%>’>");
document.write("<input type=’submit’ class=button value=’投票’> ");
document.write("<input type=button class=button value=’查看’ onclick=’jump(<%=id%>)’>");
document.write("</td></tr></tbody></table></form>");
function jump(id){
window.open("survey_vote.asp?id="+id,"survey")
}
<%
end if
end if
end if
closetable rs
closedb my
end if
%>
在surveycode.asp完成后,我们实现上已经确定了以下几点:
1、在survey_vote.asp中,如果querystring参数id有值,则是查看结果;
2、在survey_vote.asp中,如果form参数id有值,则要先进行统计;
3、在survey_vote.asp中,提交来的form参数res是答案的编号vote_no;
好,到这里我们已经完成了显示调查表单的部份;下面一篇我们将完成其它的部份。
续上篇
前面我们已经完成了文件设计,并写了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>
</table>
</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>
</table>
<%
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
%>
</table>
<%
closetable rs ’关闭表
closedb my ’关闭数据库
end sub
’----------------------
%>
</body>
</html>
到这里,我们已经完成了显示统计结果和列出所有调查的两个文件,最后只剩下
管理页面了,下一篇我们将来完成管理页面。
续上篇
上一篇中,我们完成了显示统计结果和调查列表的程序,最后我们来完成后台
管理页面,也是最重要的一个程序。
一、后台管理
在后台管理页面survey_manage.asp中,前面我们已经列出来它所要实现的
管理功能。管理的流程是先显示出所有调查,对于还没有开始的调查,可以进行修
改、删除;对于已经结束的调查,可以删除,不能修改;对于正在进行的调查,只
能修改它的结束时间。用一个参数action来表示动作,含义如下:
1、无参数。表示第一次进入,显示登录表单
2、login 表示执行登录
3、logout 表示执行退出登录
4、showaddquestion 表示显示增加一个调查
5、showsurvey 表示显示一个调查
6、doaddsurvey 表示执行增加一个调查
7、doaddanswer 表示执行增加一个答案
8、dodelsurvey 表示删除一个调查
9、dodelanswer 表示删除一个答案
10、domodify 表示修改一个调查及答案
<!--#include file="inc.asp" -->
<%
opendb my ’打开数据库
’获取参数。action表示动作,分别对应上面的功能。
action=request.querystring("action")
id=request.querystring("id")
’获取当前文件名
scr=Request.ServerVariables("SCRIPT_NAME")
’根据动作来转向相应的子程序
select case action
case "login"
login() ’执行登录
case "logout"
logout() ’执行退出登录
case "doaddsurvey"
doaddsurvey() ’执行增加一个调查
case "dodelsurvey"
dodelsurvey() ’执行删除一个调查
case "dodelanswer"
dodelanswer() ’执行删除一个答案
case "domodify"
domodify() ’执行修改一个调查及答案
end select
’----登录子程序----
sub login()
username=request.form("username") ’获取用户名
password=request.form("password") ’获取密码
if username<>"" and password<>"" then
sql="select * from manage where manage_username=’"& username &"’" ’查询用户
searchtable my,sql,rs
if not rs.eof then ’如果有
if rs("manage_password")=password then ’密码也正确
session("survey_login")=true ’登录
end if
end if
closetable rs ’关闭表
end if
response.redirect scr ’不管有没登录,最后都回到管理页
end sub
’----退出登录子程序----
sub logout()
’删除session变量
session.contents.remove "survey_login"
response.redirect scr ’回到管理页面
end sub
’----执行增加调查子程序----
sub doaddsurvey()
question=request.form("question")
stime=request.form("stime")
etime=request.form("etime")
stype=request.form("stype")
if question<>"" and stime<>"" and etime<>"" and isdate(stime)_
and isdate(etime) and session("survey_login") then
sql="select * from survey where survey_id is null"
changetable my,sql,rs
rs.addnew
rs("survey_question")=question
rs("survey_stime")=cdate(stime)
rs("survey_etime")=cdate(etime)
rs("survey_type")=cbool(stype)
rs.update
id=rs("survey_id")
closetable rs
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end if
response.redirect scr ’回到显示页面
end sub
’----执行增加调查答案子程序----
sub doaddanswer()
answer=request.form("newanswer")
if session("survey_login") then
sql="select * from survey_vote where vote_no is null"
changetable my,sql,rs
rs.addnew
rs("vote_answer")=answer
rs("vote_id")=id
rs.update
closetable rs
end if
’response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
’----执行修改调查子程序----
sub domodify()
question=request.form("question")
stime=request.form("stime")
etime=request.form("etime")
stype=request.form("stype")
answer=request.form("newanswer")
if session("survey_login") then
sql="select * from survey where survey_id="&id
changetable my,sql,rs
if not rs.eof then
if question<>"" then rs("survey_question")=question
if stime<>"" and isdate(stime) then rs("survey_stime")=cdate(stime)
if etime<>"" and isdate(etime) then
if cdate(etime)>rs("survey_stime") then rs("survey_etime")=cdate(etime)
end if
if stype<>"" then rs("survey_type")=cbool(stype)
rs.update
end if
closetable rs
if answer<>"" then doaddanswer()
sql="select vote_answer from survey_vote where vote_id="&id
changetable my,sql,rs
for i=1 to rs.recordcount
if request.form("no"&i) <>"" then
txt=request.form("txt"&i)
response.write "修改了第"&i&"条记录的内容为:"&txt&br
’把修改的内容写入数据库
rs("vote_answer")=txt
rs.update
end if
rs.movenext
next
closetable rs ’关闭数据库和表
end if
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
’----执行删除调查问题子程序----
sub dodelsurvey()
id=request.form("id")
if session("survey_login") then
sql="select * from survey where survey_id in ("&id&")"
changetable my,sql,rs
do while not rs.eof
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
if stat<>"进行中" then
sql="delete from survey_vote where vote_id="&rs("survey_id")
my.execute sql
rs.delete
end if
rs.movenext
loop
end if
response.redirect scr ’回到管理页面
end sub
’----执行删除调查答案子程序----
sub dodelanswer()
no=request.querystring("no")
if session("survey_login") then
sql="delete from survey_vote where vote_no="&no
my.execute sql
end if
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
%>
<html>
<head>
<title>在线调查后台管理</title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<%
’判断是否登录,没有则显示登录表单
if session("survey_login")<>true then
showlogin()
else
showtop() ’显示已经登录的信息
’根据动作显示相应表单
select case action
case "showsurvey"
showsurvey() ’显示单个调查
case "showaddsurvey"
showaddsurvey() ’显示增加调查表单
case ""
showall() ’显示所有调查
end select
end if
closedb my ’关闭数据库
’----显示已经登录的信息----
sub showtop()
%>
<table width="750" border="0" align="center">
<tr valign="bottom" height=20>
<td width=50 align="center"><a href="<%=scr%>">返回</a>
<td align="center"><B>在线调查后台管理程序</B></td>
<td width=50 align="center"><a href="<%=scr%>?action=logout">退出</a>
</td>
</tr>
<table>
<%
end sub
’----显示所有调查子程序----
sub showall()
opentable my,"survey",rs ’直接打开表
’下面用表格显示每个记录
’先显示表头
%>
<form action="<%=scr%>?action=dodelsurvey" method=’post’>
<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 ’查询
if isnull(tmprs("total")) then
total="-"
else
total=tmprs("total")
end if
closetable tmprs ’关闭表
’下面输出一条记录
%>
<tr >
<td align="center" height="20">
<INPUT TYPE="checkbox" NAME="id" value="<%=id%>"></td>
<td height="25">
<a href="<%=scr%>?action=showsurvey&id=<%=id%>" title="点击此处编辑">
<%=question%></a>
</td>
<td align="center" height="25"><%=stype%></td>
<td align="center" height="25"><%=stime%></td>
<td align="center" height="25"><%=etime%></td>
<td align="center" height="25"><%=stat%></td>
<td align="center" height="25"><%=total%></td>
<td align="center" height="25">
<a href="survey_vote.asp?id=<%=id%>" target="_blank">查看</a>
</td>
</tr>
<%
rs.movenext
loop
%>
<tr>
<td colspan="8" align="center">
<INPUT TYPE="submit" value="删除记录">
<INPUT TYPE="reset" value="重新选择">
<INPUT TYPE="button" value="增加调查"
onclick="window.location=’<%=scr%>?action=showaddsurvey’">
</td>
</tr>
</table>
</form>
<%
closetable rs
end sub
’----显示单个调查子程序----
sub showsurvey()
’定义SQL语句,得到调查
sql="select * from survey where survey_id=" & id
searchtable my,sql,rs ’执行查询
if not rs.eof then
’先读出每个字段
question=rs("survey_question")
stime=rs("survey_stime")
etime=rs("survey_etime")
’读出类型
if rs("survey_type") then
stype="多选"
else
stype="单选"
end if
’判断状态
if now()<stime then
stat="未开始"
else
if now<etime then
stat="进行中"
else
stat="已结束"
end if
end if
%>
<form name="addnew" action="<%=scr%>?action=domodify&id=<%=id%>" method=’post’>
<table width="760" border="1" cellspacing="0" cellpadding="2" align="center"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr height=25>
<td colspan="2" align="center"><b>修改调查</b></td>
</tr>
<tr height=25>
<td align="right" width="300">编号:</td><td><%=id%></td>
</tr>
<tr height=25>
<td align="right" width="300">问题:</td><td>
<%if stat<>"未开始" then
response.write question
else%>
<INPUT size=40 TYPE="text" NAME="question" value="<%=question%>">
<%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">起启时间:</td><td>
<%if stat<>"未开始" then
response.write stime
else%>
<INPUT TYPE="text" NAME="stime" value="<%=stime%>"><%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">结束时间:</td><td>
<%if stat="已结束" then
response.write etime
else%>
<INPUT TYPE="text" NAME="etime" value="<%=etime%>"><%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">类型:</td>
<td><%if stat<>"未开始" then
response.write stype
else%>
<INPUT TYPE="radio" NAME="stype" value="false"
<%if not rs("survey_type") then response.write "checked"%>>单选
<INPUT TYPE="radio" NAME="stype" value="true"
<%if rs("survey_type") then response.write "checked"%>>多选<%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">状态:</td><td><%=stat%></td>
</tr>
<tr>
<td valign="top" align="right" width="300">答案:</td>
<td><%
sql="select vote_no,vote_answer from survey_vote where vote_id=" & id
searchtable my,sql,tmprs ’执行查询
if tmprs.recordcount=0 then
response.write " "
else
for i=1 to tmprs.recordcount
no=tmprs(&, quot;vote_no")
answer=tmprs("vote_answer")
if stat="已结束" then
response.write i&"、"&answer&br&vbcrlf
else
response.write i&"、<INPUT TYPE=’checkbox’ NAME=’no"&i&"’ value=’"&no&"’> "
response.write vbcrlf&"<INPUT TYPE=’text’ NAME=’txt"&i&"’ value=’"&answer&"’> "
response.write vbcrlf&"<a href=’"&scr&"?action=dodelanswer&id="&id&"&no="&no&"’>删除</a>"&br
end if
tmprs.movenext
next
end if
closetable tmprs
%></td>
</tr>
<%if stat<>"已结束" then%>
<tr height=25>
<td align="right" width="300">增加答案:</td><td>
<INPUT TYPE="text" NAME="newanswer">
</td>
</tr><%end if%>
<tr>
<td colspan="2" align="center">
<INPUT TYPE="submit" value=" 确 定 ">
<INPUT TYPE="reset" value= "重新选择">
</td>
</tr>
</table>
</from>
<%
end if
closetable rs
end sub
’----显示增加调查子程序----
sub showaddsurvey()
%>
<form action="<%=scr%>?action=doaddsurvey" method=’post’>
<table width="760" border="1" cellspacing="0" cellpadding="2" align="center"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr height=25>
<td colspan="2" align="center"><b>增加调查</b></td>
</tr>
<tr height=25>
<td align="right" width="300">问题:</td>
<td><INPUT TYPE="text" NAME="question"></td>
</tr>
<tr height=25>
<td align="right" width="300">起启时间:</td><td>
<INPUT TYPE="text" NAME="stime" value="<%=DateAdd("h",1,now())%>"> 默认为1小时后</td>
</tr>
<tr height=25>
<td align="right" width="300">结束时间:</td><td>
<INPUT TYPE="text" NAME="etime" value="<%=DateAdd("m",1,now())%>"> 默认为1个月后</td>
</tr>
<tr height=25>
<td align="right" width="300">类型:</td><td>
<INPUT TYPE="radio" NAME="stype" value="false" checked>单选
<INPUT TYPE="radio" NAME="stype" value="true">多选</td>
</tr>
<tr>
<td colspan="2" align="center">
<INPUT TYPE="submit" value=" 确 定 ">
<INPUT TYPE="reset" value= " 重 写 ">
</td>
</tr>
</table>
</from>
<%
end sub
’----显示登录表单子程序----
sub showlogin()
%>
<form action="<%=scr%>?action=login" method=post>
<table width="500" border="1" align="center" cellpadding="2" cellspacing="0"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr>
<td colspan=’2’ align="center"><B>管理员登录</B></td>
</tr>
<tr>
<td align="right" width=200>用户名:</td>
<td align="left" width=300>
<INPUT TYPE="text" NAME="username" size=15 maxlength=15>
</td>
</tr>
<tr>
<td align="right" width=200>密码:</td>
<td align="left" width=300>
<INPUT TYPE="password" NAME="password" size=15 maxlength=15>
</td>
</tr>
<tr>
<td colspan=’2’ align="center">
<INPUT TYPE="submit" value="登录"> <INPUT TYPE="reset" value="重写">
</td>
</tr>
</table>
</form>
<%
end sub
%>
</body>
</html>
二、后话
到这里我们已经完成了所有的页面。当然在管理页面中,有些规则还是要根
据个人的使用情况来定,比如对于正在进行的调查,你要改成可删除,也是可以的。
另外,还可以扩展一些功能,比如:
1、可以把调查改成不能同时进行。即同一时间只能有一个调查在进行中。
2、可以修改显示调查表单的代码,改为自动选择一个正在进行的调查来显示。
结合上面两点功能,你可以做出一个能预先安排一整年调查的系统来。
3、在确定结束时间的地方,你可以改为用一个文本框来输入调查的时间长度。
当然,如果这样的话,需要加一个下拉框来选择单位,单位可以是小时、天、星期、
月、季度、年。这样的组合灵活性更高。
4、你可以更改投票的限制。比如改为一个IP投票一次。
5、你可以给每个投票指定一个管理员。这样一来就成为一个多用户的调查系统了。
更多的功能等待朋友们自己去发挥想像。
关于在线调查大家一定不会陌生吧,给出一个问题和数个答案,让用户
填写,然后把结果保存到数据库,自动进行统计,最后给出个统计的图。这
期的跟我学做就来做一个在线调查系统。
一、功能设计
这么简单的系统也要做功能设计?有的人也许会觉得奇怪,不过话说回来
不管怎么样的系统,先做功能设计总是能对系统有个比较清析的了解。让我们
来看看在线调查的功能吧。基本的功能上面已经说了,就是要给出一个问题和数
个答案,然后统计,最后给出图。在这个基础上,我们可以考虑给一个调查加上
一个时间段(有效期),在这个时间段里调查是有效的,过了这段时间就自动结
束这个调查。另外,我们可以指定一个用户一次只能提交一次答案。如果你要限
制得更多,可以指定一个IP只能提交一次答案,不过,这样可能一个网吧里的人
只能有一个提交了。对于调查里的问题,有些可能是单选题,而有些可能是多选
题。最后来说说统计的图,在统计图里要给出答案、每个答案的投票人数,并要
显示出每个答案投票数所占的比例。一般用横的图就可以了,也比较容易实现,
当然,如果你要改成竖的,也可以。
现在根据上面总结出在线调查的功能如下:
1、数据保存在ACCESS 2000 数据库中;
2、每个用户一次访问可以投票一次
3、给出每个调查的统计情况,用统计图来显示
4、每个调查都有个有效期,过期后自动结束。结束了的调查只能查看结果。
5、管理员可以增加调查,修改调查的答案(增加、修改、删除,修改类型)。
6、对于已经结束的调查,管理员只能删除调查,而不能修改答案。
7、只有一个管理员(单用户)
二、数据库设计
现在来设计数据库,根据功能要求,至少要有三个表,一是管理员表,二是
调查表,三是调查结果表。数据库文件名为survey.mdb 可以改为.asp 如果改
的话,请在ASP程序中作相应的修改。
表一、 管理员表 表名: manage
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
manage_id 自动编号 - 在这里没用到,日后扩展用
manage_username 文本 15 管理员用户名
manage_password 文本 15 管理员密码
-----------------------------------------------------------------
建立manage表后加入一条新记录,填入你的管理员用户名和密码,在这里填入的是xmxoxo
表二、 调查表 表名: survey
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
survey_id 自动编号 - 递增、主键、有索引无重复
survey_question 文本 255 调查问题
survey_type 是否 - 类型,否:单选 是:多选
survey_stime 日期 - 长日期,开始时间
survey_etime 日期 - 长日期,结束时间
-----------------------------------------------------------------
表三、调查结束表 表名:survey_vote
-----------------------------------------------------------------
字段 类型 长度 说明
-----------------------------------------------------------------
vote_no 自动编号 - 递增、主键、有索引无重复
vote_id 长整型 - 有索引有重复,小数位0
vote_answer 文本 100 调查答案
vote_count 长整型 - 投票数
-----------------------------------------------------------------
其中,survey_vote表和survey表的id字段有多对一的关系。并不一定要建立这个关系,
但是建立关系会使思路更明确。
三、包含文件
这里所要用到的函数并不多,主要是对数据库进行操作的,如果要防止输入时的
HTML等代码,直接用server.htmlencode进行处理就可以了,所以不需要一个专门的
函数来处理。我们可以沿用上一篇《跟我学》系列《跟我学做树型菜单》里的包含文件。
共用函数文件,文件名:inc.asp
<%
’*******************************************************************
’通用数据库ASP函数
’*******************************************************************
’数据库常数
databasename="survey.mdb" ’数据库名,如果改名的话,在这里修改就行了
’*******************************************************************
’打开数据库
sub opendb(connect)
set connect=server.CreateObject("ADODB.connection")
connect.ConnectionString="DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" &_
server.MapPath(databasename)
connect.Open strconn
end sub
’*******************************************************************
’关闭数据库
sub closedb(connect)
connect.close
set connect=nothing
end sub
’*******************************************************************
’打开单个表读
sub opentable(connect,tbname,myrs)
set myrs=server.createobject("ADODB.recordset")
rssql="select * from " & tbname
myrs.open rssql,connect,1,1
end sub
’*******************************************************************
’关闭临时表
sub closetable(rs)
rs.close
set rs=nothing
end sub
’*******************************************************************
’查询数据库
sub searchtable(connect,sql,rs)
set rs=server.createobject("ADODB.recordset")
rs.open sql,connect,1,1
end sub
’*******************************************************************
’查询并更改数据库
sub changetable(connect,sql,rs)
set rs=server.createobject("ADODB.recordset")
rs.open sql,connect,1,3
end sub
’*******************************************************************
’显示信息 用于调试
Sub w(msg)
response.write msg
end sub
’*******************************************************************
’程序中断 用于调试
sub userstop()
response.end
end sub
%>
到这里暂告一段落,我们已经完成了调查系统的功能、数据库及函数的设计,下一篇
我们来做文件设计,并逐个完成。再次申明,所有的《跟我学做》系列都仅是给朋友们一个
学习的机会,不欢迎索取完整的程序。对于图片等内容,会适当给出下载地址。为了方便大家
学习,下一篇会尽快发布。
续上篇
上一篇中我们已经分析了在线调查的功能,数据库结构以及所要用到的一些
函数。这一篇的主要任务就是文件的设计。设计出要有多少个文件来构成这个在线
调查,每个文件又分担什么任务。并写出这些文件的主要流程,以方便后面的代码编
写。
一、文件设计
按照传统的思路,我们总是把在线调查设计成三个部份,一是显示调查表;二
是显示调查统计结果;三是后台管理。而在实际应用中,我们会发现,显示调查表
往往是在某个网页中的,显示结果一般也是弹出一个窗口来。后台管理则是通过另一
个链接或者登录表单转向到的。为了更方便地使用在线调查,我们把显示调查表部分
写成一个JS脚本,让需要显示调查表的网页通过引用脚本的方式来自由地调用它。好,
看看文件设计
1、inc.asp 包含文件。主要的函数库
2、surveycode.asp 显示调查表程序。在主网页中使用脚本来调用它。
3、survey.asp 调查表列表程序。列出所有调查的状态。
4、survey_vote.asp 显示调查统计结果程序。带上参数表示调查的问题。
5、survey_manage.asp 管理程序。
在这里,我们略过了页面设计,因为风格设计不是我们所要说的内容,所以关于网
页的风格,布局,CSS 等等,请大家自己设计。
二、文件流程
先写出程序的主要流程,可以比较方便于日后修改和扩展、移植。在这里比较重要的
是要在程序中写哪些子程序,如何安排这些子程序。
1、surveycode.asp 显示调查表单
<%
’获取querystring参数,id 表示调查编号
’判断参数正确性
’判断调查是否在有效期中。
’读取调查问题、类型
’输出调查答案,并生成调查表单
’关闭数据库及表
%>
2、survey.asp 显示所有调查状态
<%
’读取数据库
%>
<html>
’显示所有调查状态,并加入链接
</html>
3、survey_vote.asp 显示统计结果。
这里有两个功能,一是没有提交的显示,二是提交了答案后进行统计,然后显示
结果。如果不带参数,就是第一种方式。也可以分为两个文件来完成。
<%
’获取参数。id 表示调查编号 所有数据来自form
’判断是否有参数,有则先进行统计
’没有则直接显示
’统计子程序
%>
<html>
’显示子程序
</html>
4、survey_manage.asp 管理程序。
管理部份比较复杂,要实现较多的功能。先来列一下管理的功能:
1)管理员登录。只有登录后才能进行管理
2)退出登录。完成管理后安全退出。
对调查的管理有:
3)增加一个调查。同时要增加调查答案
4)修改一个调查。修改内容,时间,类型,调查答案的内容、增加、删除
5)删除一个调查。不能删除正在进行的调查。
针对这些功能,来设计它的流程
<%
’获取参数。action表示动作,分别对应上面的功能。
’根据动作来转向相应的子程序
’登录子程序
’退出登录子程序
’执行增加调查问题子程序
’执行增加调查答案子程序
’执行修改调查子程序 问题和答案一起修改
’执行删除调查问题子程序
’执行删除调查答案子程序
<html>
<%
’判断是否登录,没有则显示登录表单
’根据动作显示相应表单
’显示所有调查子程序
’显示单个调查子程序。问题和答案一起显示
’显示增加调查子程序。
’显示登录表单
%>
</html>
三、代码编写
做好了流程设计后,写代码就比较有条理了。让我们从简单的开始。在编写代码
之前,我们要先在数据库里输入一些记录,以便做测试。先加入一条调查问题,和几个
调查答案,并手工输入一些统计信息。
我们先来写显示调查表单的surveycode.asp 这个文件要在其它页面中被调用,
所以我们写成JS和VBS混用的方式。调用的时候可以把它放在某个表格中,用下面的语句:
<SCRIPT Language="javascript" SRC="surveycode.asp?id=1"></SCRIPT>
按照上面的流程,在显示表单前,先要判断一下调查是否存在,是否在进行中。另外,
在表单中要提交一个隐藏的参数,来表示调查的问题编号(id),答案提交的时候,提交的
是答案的编号vote_no
文件名 surveycode.asp
<!--#include file="inc.asp" -->
<%
id=request.querystring("id")
if id<>"" then ’如果有参数
opendb my ’联接数据库
sql="select * from survey where survey_id="& id ’查询语句
searchtable my,sql,rs ’查询数据库
if not rs.eof then ’如果有这个调查记录
question=rs("survey_question") ’读出问题
surveytype=rs("survey_type") ’读出答案类型
stime=rs("survey_stime") ’读出开始时间
etime=rs("survey_etime") ’读出结束时间
closetable rs ’关闭表
if stime<now() and etime>now() then ’如果调查正在进行中
’下面输出调查表单
’先输出表单和问题,表单提交到survey_vote.asp
%>
document.write("<form action=’survey_vote.asp’ target=’_blank’ method=’post’>");
document.write("<table border=’1’ cellpadding=’2’ cellspacing=0’ bordercolorligh=’#000000’");
document.write(" bordercolordark=’#ffffff’ width=’100%’ align=’center’><tbody>");
document.write("<tr><td colspan=’2’ align=’center’><b><%=server.htmlencode(question)%></b></td></tr>");
<%
sql="select vote_no,vote_answer from survey_vote where vote_id="&id ’查询答案的SQL
searchtable my,sql,rs ’执行查询
if not rs.eof then ’如果有答案,就输出
for i=1 to rs.recordcount
%>
document.write("<tr><td align=’right’><input name=’res’ type=’");
<%
if surveytype then ’判断类型,显示单选或者多选
%>
document.write("checkbox");
<%else%>
document.write("radio");
<%end if ’下面这句输出答案的文字和提交的值(vote_no)%>
document.write("’ value=<%=rs("vote_no")%>></td><td><%=rs("vote_answer")%></td></tr>");
<%
rs.movenext
next
’下面几句输出一个隐藏的参数,传递问题编号(id)
’并用一个JS函数来定义点击查看后的链接
%>
document.write("<tr><td colspan=’2’ align=’center’><input type=’hidden’ name=’id’ value=’<%=id%>’>");
document.write("<input type=’submit’ class=button value=’投票’> ");
document.write("<input type=button class=button value=’查看’ onclick=’jump(<%=id%>)’>");
document.write("</td></tr></tbody></table></form>");
function jump(id){
window.open("survey_vote.asp?id="+id,"survey")
}
<%
end if
end if
end if
closetable rs
closedb my
end if
%>
在surveycode.asp完成后,我们实现上已经确定了以下几点:
1、在survey_vote.asp中,如果querystring参数id有值,则是查看结果;
2、在survey_vote.asp中,如果form参数id有值,则要先进行统计;
3、在survey_vote.asp中,提交来的form参数res是答案的编号vote_no;
好,到这里我们已经完成了显示调查表单的部份;下面一篇我们将完成其它的部份。
续上篇
前面我们已经完成了文件设计,并写了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>
</table>
</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>
</table>
<%
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
%>
</table>
<%
closetable rs ’关闭表
closedb my ’关闭数据库
end sub
’----------------------
%>
</body>
</html>
到这里,我们已经完成了显示统计结果和列出所有调查的两个文件,最后只剩下
管理页面了,下一篇我们将来完成管理页面。
续上篇
上一篇中,我们完成了显示统计结果和调查列表的程序,最后我们来完成后台
管理页面,也是最重要的一个程序。
一、后台管理
在后台管理页面survey_manage.asp中,前面我们已经列出来它所要实现的
管理功能。管理的流程是先显示出所有调查,对于还没有开始的调查,可以进行修
改、删除;对于已经结束的调查,可以删除,不能修改;对于正在进行的调查,只
能修改它的结束时间。用一个参数action来表示动作,含义如下:
1、无参数。表示第一次进入,显示登录表单
2、login 表示执行登录
3、logout 表示执行退出登录
4、showaddquestion 表示显示增加一个调查
5、showsurvey 表示显示一个调查
6、doaddsurvey 表示执行增加一个调查
7、doaddanswer 表示执行增加一个答案
8、dodelsurvey 表示删除一个调查
9、dodelanswer 表示删除一个答案
10、domodify 表示修改一个调查及答案
<!--#include file="inc.asp" -->
<%
opendb my ’打开数据库
’获取参数。action表示动作,分别对应上面的功能。
action=request.querystring("action")
id=request.querystring("id")
’获取当前文件名
scr=Request.ServerVariables("SCRIPT_NAME")
’根据动作来转向相应的子程序
select case action
case "login"
login() ’执行登录
case "logout"
logout() ’执行退出登录
case "doaddsurvey"
doaddsurvey() ’执行增加一个调查
case "dodelsurvey"
dodelsurvey() ’执行删除一个调查
case "dodelanswer"
dodelanswer() ’执行删除一个答案
case "domodify"
domodify() ’执行修改一个调查及答案
end select
’----登录子程序----
sub login()
username=request.form("username") ’获取用户名
password=request.form("password") ’获取密码
if username<>"" and password<>"" then
sql="select * from manage where manage_username=’"& username &"’" ’查询用户
searchtable my,sql,rs
if not rs.eof then ’如果有
if rs("manage_password")=password then ’密码也正确
session("survey_login")=true ’登录
end if
end if
closetable rs ’关闭表
end if
response.redirect scr ’不管有没登录,最后都回到管理页
end sub
’----退出登录子程序----
sub logout()
’删除session变量
session.contents.remove "survey_login"
response.redirect scr ’回到管理页面
end sub
’----执行增加调查子程序----
sub doaddsurvey()
question=request.form("question")
stime=request.form("stime")
etime=request.form("etime")
stype=request.form("stype")
if question<>"" and stime<>"" and etime<>"" and isdate(stime)_
and isdate(etime) and session("survey_login") then
sql="select * from survey where survey_id is null"
changetable my,sql,rs
rs.addnew
rs("survey_question")=question
rs("survey_stime")=cdate(stime)
rs("survey_etime")=cdate(etime)
rs("survey_type")=cbool(stype)
rs.update
id=rs("survey_id")
closetable rs
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end if
response.redirect scr ’回到显示页面
end sub
’----执行增加调查答案子程序----
sub doaddanswer()
answer=request.form("newanswer")
if session("survey_login") then
sql="select * from survey_vote where vote_no is null"
changetable my,sql,rs
rs.addnew
rs("vote_answer")=answer
rs("vote_id")=id
rs.update
closetable rs
end if
’response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
’----执行修改调查子程序----
sub domodify()
question=request.form("question")
stime=request.form("stime")
etime=request.form("etime")
stype=request.form("stype")
answer=request.form("newanswer")
if session("survey_login") then
sql="select * from survey where survey_id="&id
changetable my,sql,rs
if not rs.eof then
if question<>"" then rs("survey_question")=question
if stime<>"" and isdate(stime) then rs("survey_stime")=cdate(stime)
if etime<>"" and isdate(etime) then
if cdate(etime)>rs("survey_stime") then rs("survey_etime")=cdate(etime)
end if
if stype<>"" then rs("survey_type")=cbool(stype)
rs.update
end if
closetable rs
if answer<>"" then doaddanswer()
sql="select vote_answer from survey_vote where vote_id="&id
changetable my,sql,rs
for i=1 to rs.recordcount
if request.form("no"&i) <>"" then
txt=request.form("txt"&i)
response.write "修改了第"&i&"条记录的内容为:"&txt&br
’把修改的内容写入数据库
rs("vote_answer")=txt
rs.update
end if
rs.movenext
next
closetable rs ’关闭数据库和表
end if
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
’----执行删除调查问题子程序----
sub dodelsurvey()
id=request.form("id")
if session("survey_login") then
sql="select * from survey where survey_id in ("&id&")"
changetable my,sql,rs
do while not rs.eof
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
if stat<>"进行中" then
sql="delete from survey_vote where vote_id="&rs("survey_id")
my.execute sql
rs.delete
end if
rs.movenext
loop
end if
response.redirect scr ’回到管理页面
end sub
’----执行删除调查答案子程序----
sub dodelanswer()
no=request.querystring("no")
if session("survey_login") then
sql="delete from survey_vote where vote_no="&no
my.execute sql
end if
response.redirect scr&"?action=showsurvey&id="&id ’回到显示页面
end sub
%>
<html>
<head>
<title>在线调查后台管理</title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<%
’判断是否登录,没有则显示登录表单
if session("survey_login")<>true then
showlogin()
else
showtop() ’显示已经登录的信息
’根据动作显示相应表单
select case action
case "showsurvey"
showsurvey() ’显示单个调查
case "showaddsurvey"
showaddsurvey() ’显示增加调查表单
case ""
showall() ’显示所有调查
end select
end if
closedb my ’关闭数据库
’----显示已经登录的信息----
sub showtop()
%>
<table width="750" border="0" align="center">
<tr valign="bottom" height=20>
<td width=50 align="center"><a href="<%=scr%>">返回</a>
<td align="center"><B>在线调查后台管理程序</B></td>
<td width=50 align="center"><a href="<%=scr%>?action=logout">退出</a>
</td>
</tr>
<table>
<%
end sub
’----显示所有调查子程序----
sub showall()
opentable my,"survey",rs ’直接打开表
’下面用表格显示每个记录
’先显示表头
%>
<form action="<%=scr%>?action=dodelsurvey" method=’post’>
<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 ’查询
if isnull(tmprs("total")) then
total="-"
else
total=tmprs("total")
end if
closetable tmprs ’关闭表
’下面输出一条记录
%>
<tr >
<td align="center" height="20">
<INPUT TYPE="checkbox" NAME="id" value="<%=id%>"></td>
<td height="25">
<a href="<%=scr%>?action=showsurvey&id=<%=id%>" title="点击此处编辑">
<%=question%></a>
</td>
<td align="center" height="25"><%=stype%></td>
<td align="center" height="25"><%=stime%></td>
<td align="center" height="25"><%=etime%></td>
<td align="center" height="25"><%=stat%></td>
<td align="center" height="25"><%=total%></td>
<td align="center" height="25">
<a href="survey_vote.asp?id=<%=id%>" target="_blank">查看</a>
</td>
</tr>
<%
rs.movenext
loop
%>
<tr>
<td colspan="8" align="center">
<INPUT TYPE="submit" value="删除记录">
<INPUT TYPE="reset" value="重新选择">
<INPUT TYPE="button" value="增加调查"
onclick="window.location=’<%=scr%>?action=showaddsurvey’">
</td>
</tr>
</table>
</form>
<%
closetable rs
end sub
’----显示单个调查子程序----
sub showsurvey()
’定义SQL语句,得到调查
sql="select * from survey where survey_id=" & id
searchtable my,sql,rs ’执行查询
if not rs.eof then
’先读出每个字段
question=rs("survey_question")
stime=rs("survey_stime")
etime=rs("survey_etime")
’读出类型
if rs("survey_type") then
stype="多选"
else
stype="单选"
end if
’判断状态
if now()<stime then
stat="未开始"
else
if now<etime then
stat="进行中"
else
stat="已结束"
end if
end if
%>
<form name="addnew" action="<%=scr%>?action=domodify&id=<%=id%>" method=’post’>
<table width="760" border="1" cellspacing="0" cellpadding="2" align="center"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr height=25>
<td colspan="2" align="center"><b>修改调查</b></td>
</tr>
<tr height=25>
<td align="right" width="300">编号:</td><td><%=id%></td>
</tr>
<tr height=25>
<td align="right" width="300">问题:</td><td>
<%if stat<>"未开始" then
response.write question
else%>
<INPUT size=40 TYPE="text" NAME="question" value="<%=question%>">
<%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">起启时间:</td><td>
<%if stat<>"未开始" then
response.write stime
else%>
<INPUT TYPE="text" NAME="stime" value="<%=stime%>"><%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">结束时间:</td><td>
<%if stat="已结束" then
response.write etime
else%>
<INPUT TYPE="text" NAME="etime" value="<%=etime%>"><%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">类型:</td>
<td><%if stat<>"未开始" then
response.write stype
else%>
<INPUT TYPE="radio" NAME="stype" value="false"
<%if not rs("survey_type") then response.write "checked"%>>单选
<INPUT TYPE="radio" NAME="stype" value="true"
<%if rs("survey_type") then response.write "checked"%>>多选<%end if%></td>
</tr>
<tr height=25>
<td align="right" width="300">状态:</td><td><%=stat%></td>
</tr>
<tr>
<td valign="top" align="right" width="300">答案:</td>
<td><%
sql="select vote_no,vote_answer from survey_vote where vote_id=" & id
searchtable my,sql,tmprs ’执行查询
if tmprs.recordcount=0 then
response.write " "
else
for i=1 to tmprs.recordcount
no=tmprs(&, quot;vote_no")
answer=tmprs("vote_answer")
if stat="已结束" then
response.write i&"、"&answer&br&vbcrlf
else
response.write i&"、<INPUT TYPE=’checkbox’ NAME=’no"&i&"’ value=’"&no&"’> "
response.write vbcrlf&"<INPUT TYPE=’text’ NAME=’txt"&i&"’ value=’"&answer&"’> "
response.write vbcrlf&"<a href=’"&scr&"?action=dodelanswer&id="&id&"&no="&no&"’>删除</a>"&br
end if
tmprs.movenext
next
end if
closetable tmprs
%></td>
</tr>
<%if stat<>"已结束" then%>
<tr height=25>
<td align="right" width="300">增加答案:</td><td>
<INPUT TYPE="text" NAME="newanswer">
</td>
</tr><%end if%>
<tr>
<td colspan="2" align="center">
<INPUT TYPE="submit" value=" 确 定 ">
<INPUT TYPE="reset" value= "重新选择">
</td>
</tr>
</table>
</from>
<%
end if
closetable rs
end sub
’----显示增加调查子程序----
sub showaddsurvey()
%>
<form action="<%=scr%>?action=doaddsurvey" method=’post’>
<table width="760" border="1" cellspacing="0" cellpadding="2" align="center"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr height=25>
<td colspan="2" align="center"><b>增加调查</b></td>
</tr>
<tr height=25>
<td align="right" width="300">问题:</td>
<td><INPUT TYPE="text" NAME="question"></td>
</tr>
<tr height=25>
<td align="right" width="300">起启时间:</td><td>
<INPUT TYPE="text" NAME="stime" value="<%=DateAdd("h",1,now())%>"> 默认为1小时后</td>
</tr>
<tr height=25>
<td align="right" width="300">结束时间:</td><td>
<INPUT TYPE="text" NAME="etime" value="<%=DateAdd("m",1,now())%>"> 默认为1个月后</td>
</tr>
<tr height=25>
<td align="right" width="300">类型:</td><td>
<INPUT TYPE="radio" NAME="stype" value="false" checked>单选
<INPUT TYPE="radio" NAME="stype" value="true">多选</td>
</tr>
<tr>
<td colspan="2" align="center">
<INPUT TYPE="submit" value=" 确 定 ">
<INPUT TYPE="reset" value= " 重 写 ">
</td>
</tr>
</table>
</from>
<%
end sub
’----显示登录表单子程序----
sub showlogin()
%>
<form action="<%=scr%>?action=login" method=post>
<table width="500" border="1" align="center" cellpadding="2" cellspacing="0"
bordercolorligh="#000000" bordercolordark="#ffffff">
<tr>
<td colspan=’2’ align="center"><B>管理员登录</B></td>
</tr>
<tr>
<td align="right" width=200>用户名:</td>
<td align="left" width=300>
<INPUT TYPE="text" NAME="username" size=15 maxlength=15>
</td>
</tr>
<tr>
<td align="right" width=200>密码:</td>
<td align="left" width=300>
<INPUT TYPE="password" NAME="password" size=15 maxlength=15>
</td>
</tr>
<tr>
<td colspan=’2’ align="center">
<INPUT TYPE="submit" value="登录"> <INPUT TYPE="reset" value="重写">
</td>
</tr>
</table>
</form>
<%
end sub
%>
</body>
</html>
二、后话
到这里我们已经完成了所有的页面。当然在管理页面中,有些规则还是要根
据个人的使用情况来定,比如对于正在进行的调查,你要改成可删除,也是可以的。
另外,还可以扩展一些功能,比如:
1、可以把调查改成不能同时进行。即同一时间只能有一个调查在进行中。
2、可以修改显示调查表单的代码,改为自动选择一个正在进行的调查来显示。
结合上面两点功能,你可以做出一个能预先安排一整年调查的系统来。
3、在确定结束时间的地方,你可以改为用一个文本框来输入调查的时间长度。
当然,如果这样的话,需要加一个下拉框来选择单位,单位可以是小时、天、星期、
月、季度、年。这样的组合灵活性更高。
4、你可以更改投票的限制。比如改为一个IP投票一次。
5、你可以给每个投票指定一个管理员。这样一来就成为一个多用户的调查系统了。
更多的功能等待朋友们自己去发挥想像。