关于在线调查大家一定不会陌生吧,给出一个问题和数个答案,让用户填写,然后把结果保存到数据库,自动进行统计,最后给出个统计的图。这期的跟我学做就来做一个在线调查系统。
一、功能设计
这么简单的系统也要做功能设计?有的人也许会觉得奇怪,不过话说回来不管怎么样的系统,先做功能设计总是能对系统有个比较清析的了解。让我们来看看在线调查的功能吧。基本的功能上面已经说了,就是要给出一个问题和数个答案,然后统计,最后给出图。在这个基础上,我们可以考虑给一个调查加上一个时间段(有效期),在这个时间段里调查是有效的,过了这段时间就自动结束这个调查。另外,我们可以指定一个用户一次只能提交一次答案。如果你要限制得更多,可以指定一个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 %> |
| <% ''获取querystring参数,id 表示调查编号 ''判断参数正确性 ''判断调查是否在有效期中。 ''读取调查问题、类型 ''输出调查答案,并生成调查表单 ''关闭数据库及表 %> |
| <% ''读取数据库 %> <html> ''显示所有调查状态,并加入链接 </html> |
| <% ''获取参数。id 表示调查编号 所有数据来自form ''判断是否有参数,有则先进行统计 ''没有则直接显示 ''统计子程序 %> <html> ''显示子程序 </html> |
| <% ''获取参数。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 %> |
| <!--#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> |
<!--#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> |