|          
续上篇上一篇中,我们完成了显示统计结果和调查列表的程序,最后我们来完成后台
 管理页面,也是最重要的一个程序。
 
 一、后台管理
 在后台管理页面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>
 
 </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("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>
 
 </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>
 
 </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>
 
 </form>
 <%
 end sub
 %>
 </body>
 </html>
 
 
 二、后话
 到这里我们已经完成了所有的页面。当然在管理页面中,有些规则还是要根
 据个人的使用情况来定,比如对于正在进行的调查,你要改成可删除,也是可以的。
 另外,还可以扩展一些功能,比如:
 1、可以把调查改成不能同时进行。即同一时间只能有一个调查在进行中。
 2、可以修改显示调查表单的代码,改为自动选择一个正在进行的调查来显示。
 结合上面两点功能,你可以做出一个能预先安排一整年调查的系统来。
 3、在确定结束时间的地方,你可以改为用一个文本框来输入调查的时间长度。
 当然,如果这样的话,需要加一个下拉框来选择单位,单位可以是小时、天、星期、
 月、季度、年。这样的组合灵活性更高。
 4、你可以更改投票的限制。比如改为一个IP投票一次。
 5、你可以给每个投票指定一个管理员。这样一来就成为一个多用户的调查系统了。
 更多的功能等待朋友们自己去发挥想像。
 |