ASP+VML+DB实现投票统计项目

网络整理 - 08-24
个月前我看到过一位网友lshdic写的一篇用JS+VML的《使用 Vml 制作立体柱状投票统计图的完整程序》。
       我觉得这个方法非常不错,可以不使用图片就生成统计图,现在就让我们一起来用ASP实现这个程序。

      准备工作:用ACCESS建立一个MDB数据库,名为vote.mdb,并且在数据库中建立如下两个表:
然后建立我们按照ASP开发的惯例建立连接数据库的文件conn.asp
<%
'conn.asp
Set conn=Server.CreateObject("ADODB.Connection")
conn.Open "driver={Microsoft Access Driver (*.mdb)};dbq="& Server.MapPath("vote.mdb")
%>

显示投票项目的列表,因为我们要制作的是一个多项目的投票系统vote_list.asp
<!--#include file="conn.asp"-->
<html>
<head>
<title>投票项目列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../images/main.css" type="text/css">
</head>
<%
set rs=conn.execute("select * from votetitle order by voteid desc")
%>
<table border=1 cellspacing="0" cellpadding="0" bgcolor="#ffffff" bordercolor="#000000" width=100%>
  <tr>
    <td bgcolor="#EFEFEF">所有投票列表</td>
  </tr>
  <%do while not rs.eof%>
  <tr>
    <td>编号:<font color="#FF0000"><%=rs("voteid")%>  </font><a href="vote.asp?voteid=<%=rs("voteid")%>" target="_blank"><%=rs("votetitle")%>
      </a>(<%=rs("time")%>)</td>
  </tr>
  <%rs.movenext 
loop 
rs.close
set rs=nothing%>
</table>
</body>
</html>

投票显示页面vote_show.asp
<%if request("voteid")="" then
response.write "参数没有指定。"
response.End()
end if%>
<!--#include file="conn.asp"-->
<html>
<head>
<title>查看结果</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<STYLE>
td{font-size:12px}
body{font-size:12px}
v\:*{behavior:url(#default#VML);} //这里声明了v作为VML公用变量
</STYLE>
<link href="../images/main.css" type="text/css">
</head>
<%
dim voteid
voteid=request("voteid")
set rs=conn.execute("select count from vote,votetitle where votetitle.voteid="&voteid&" and vote.voteid=votetitle.voteid order by id")
iii=0
do while not rs.eof
ii=ii+1
if ii=1 then
ceocio=trim(rs("count"))
ceocio1=trim("array1["&iii&"]")
else
ceocio=trim(ceocio&","&rs("count"))
ceocio1=trim(ceocio1&"+array1["&iii&"]")
end if
iii=iii+1
rs.movenext 
loop 
rs.close
set rs=nothing
'response.write ii
'response.write ceocio1
%>
<script>
array1=new Array(<%=ceocio%>) //不同的投票票数
allstr=<%=ceocio1%>
//alert(allstr)
//allstr=array1[0]+array1[1]+array1[2]+array1[3]+array1[4]+array1[5] //得到总数
for(i=0;i<=<%=(ii-1)%>;i++){
mathstr=Math.round(100/(allstr/array1[i])) //求百分之几, 100/(总和/单个)
document.write ("<v:rect fillcolor='lime'><br> %"+mathstr+"<br>"+array1[i]+"人<v:Extrusion backdepth='15pt' on='true'/></v:rect>")
}
</script>
<%
voteid=request("voteid")
set rs=conn.execute("select * from vote,votetitle where votetitle.voteid="&voteid&" and vote.voteid=votetitle.voteid order by id")
%>
<table border=1 cellspacing="0" cellpadding="2" bgcolor="#ffffff" bordercolor="#000000" width=100%>
  <tr>
    <td bgcolor="#EFEFEF">关于:<font color="#FF0000"><%=rs("votetitle")%></font>的调查结果:<br>
      共有<b><font color="#FF0000"><%=rs("total")%></font></b>人参与调查<br>
      (<%=rs("time")%>到<%=date%>)</td>
  </tr>
  <%do while not rs.eof
dim i
i=i+1%>
  <tr>
    <td>选项<%=i%>:<%=rs("title")%>     <font color="#FF0000"><%=rs("count")%>人</font></td>
  </tr>
  <%rs.movenext 
loop 
rs.close
set rs=nothing%>
  <tr>
    <td> </td>
  </tr>
</table>
<br>
<a href="vote_list.asp">查看过往投票</a> <br>
<a href="vote.asp?voteid=<%=request("voteid")%>">投票本项目</a>
</body>
</html>

vote.asp
<!--#include file="conn.asp"-->
<%if request("voteid")="" then
set rs=conn.execute("select top 1 voteid from votetitle order by voteid desc")
voteid=rs("voteid")
rs.close
set rs=nothing
else
voteid=request("voteid")
end if%>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="style.css" type="text/css">
</head>

<body>
<%
if request("action")="vote" then
     '加上","&request("voteid")","为了不使id为1的投票和id为11的投票混遐
     if instr(request.cookies("vote"),","&request("voteid")&",")=0 then
     else
        response.write "你已经投过票了"
        response.end
     end if
sql="update [vote] set count=count+1 wherevotevalue")
conn.execute(sql)
sql="update [votetitle] set total=total+1 where voteid="&request("voteid")
conn.execute(sql)
'写入cookies有效期
Response.Cookies("vote").Expires=Date+1
'将已经投票的选项全部记录在cookies,用,号隔开。
Response.Cookies("vote")=","&Request.Cookies("vote")&","&Request("voteid")&","
voteid=request("voteid")
'转向基本
response.redirect "vote_show.asp?voteid="&voteid
end if
%>
<%
'dim voteid
'voteid=request("voteid")
set rs=conn.execute("select * from vote,votetitle where votetitle.voteid="&voteid&" and vote.voteid=votetitle.voteid order by id")
%>
<table border=1 cellspacing="0" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
  <tr>
    <td colspan="2" bgcolor="#EFEFEF">调查:<%=rs("votetitle")%>(<%=rs("time")%>)</td>
  </tr><form method="post" action="vote.asp?action=vote&voteid=<%=voteid%>">
<%do while not rs.eof
dim i
i=i+1%>
  <tr>
    <td colspan="2">
        <input type="radio" value="<%=rs("id")%>" <%if i=1 then%>checked<%end if%>>
        <%=rs("title")%>   
    </td>
  </tr>
<%rs.movenext 
loop 
rs.close
set rs=nothing%>
  <tr>
      <td colspan="2">
        <%if instr(request.cookies("vote"),","&voteid&",")=0 then%>
        <input type="submit" value="Vote"></form>
        <%else%>
  <font color="#FF0000">您已经投过票了</font>
        <%end if%></td>
  </tr>
</table>
<br>
<a href="vote_list.asp">查看过往投票</a> <br>
<a href="vote_show.asp?voteid=<%=voteid%>">查看本投票的结果</a>
</body>
</html>

后台管理页面admin_vote_list.asp
<!--#include file="conn.asp"-->
<%'添加管理员权限在这里,这里为了大家应用方便,我没有加验证就直接允许进入%>
<html>
<head>
<title>投票项目列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="style.css" type="text/css">
</head>

<body>
<%
'删除模块
dim action
action=request("action")
if action="del" then
            Dim StrSQL1,StrSQL2
            StrSQL1="delete from vote where voteid="&request("voteid")
            conn.Execute StrSQL1
            StrSQL2="delete from votetitle where voteid="&request("voteid")
            conn.Execute StrSQL2
   response.Redirect "?"
end if
'修改模块
'/////////////////////
'修改表单
if action="add" then
%>
<table border=1 cellspacing="0" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
  <tr>
    <td bgcolor="#EFEFEF"><center>
        添加新的投票</center></td>
  </tr>
  <tr>
    <td>请输入这个投票所需要的选项个数。</td>
  </tr>
  <tr>
    <td><form method="post" action="?action=add1">
        <select>
  <%for i1=2 to 10%>
          <option value="<%=i1%>"><%=i1%></option>
  <%next%>
        </select>
        <input type="submit" value="Submit">
      </form></td>
  </tr>
</table>
<%
end if
if action="add1" then
num=request("num")
%>
<table border=1 cellspacing="0" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
  <tr>
    <td bgcolor="#EFEFEF"><center>
        添加新的投票</center></td>
  </tr>
  <tr>
    <td>请输入这个投票所需要的详细信息。</td>
  </tr>
  <tr>
    <td><form method="post" action="?action=add2">
        投票名称
        <input type="text">
        <br>
  <%for i2=1 to num%>
        选项<%=i2%><input type="text"><br>
        <%next%>
        <input type="hidden" value="<%=request("num")%>">
        <input type="submit" value="Submit">
        <input type="reset" value="Reset">
      </form></td>
  </tr>
</table>

<%end if
if action="add2" then
Set rs=Server.CreateObject("ADODB.Recordset")
sql="SELECT * FROM votetitle"
rs.Open sql,conn,1,3
rs.Addnew
rs("votetitle")=request("votetitle")
rs("time")=date()
rs.Update
rs.Close
set rs=conn.execute("select top 1 voteid from votetitle order by voteid desc")
voteid=rs("voteid")
rs.close
set rs=nothing
'//////////////
'/////////////循环
num=request("num")
for i3=1 to num
Set rs=Server.CreateObject("ADODB.Recordset")
sql="SELECT * FROM vote"
rs.Open sql,conn,1,3
rs.Addnew
rs("title")=request(i3)
rs("voteid")=voteid
rs.Update
rs.Close
next
'////////////循环结束
Set rs=Nothing
response.Write "<a href=admin_vote_list.asp>成功添加,请返回</a>"
end if
'修改表单结束
'////////////////////
'修改执行代码
'修改执行代码结束
%>

<%if action="" then
set rs=conn.execute("select * from votetitle order by voteid desc")
%>
<table border=1 cellspacing="0" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
  <tr>
    <td colspan="2" bgcolor="#EFEFEF">所有投票列表</td>
  </tr>
  <%do while not rs.eof%>
  <tr>
    <td>编号:<font color="#FF0000"><%=rs("voteid")%>  </font><a href="vote.asp?voteid=<%=rs("voteid")%>" target="_blank"><%=rs("votetitle")%>
      </a>(<%=rs("time")%>)</td>
    <td>【<a href="?action=del&voteid=<%=rs("voteid")%>">删除</a>】</td>
  </tr>
  <%rs.movenext 
loop 
rs.close
set rs=nothing%>
  <tr>
    <td colspan="2">【<a href="?action=add">添加一个新的投票调查项目</a>】</td>
  </tr>
</table>
</body>
</html>
<%end if%>

最后显示效果如下: