幾個月前我看到一位網友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" rel="stylesheet" type="text/css">
</head>
<%
set rs=conn.execute("select * from votetitle order by voteid desc")
%>
<table border=1 cellspacing="0" style="border-collapse: collapse" 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" rel="stylesheet" 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' style='width:20;color:navy;height:"+500*<%=(ii-1)%>/(1000/mathstr)+"' ><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" style="border-collapse: collapse" 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" rel="stylesheet" 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 where id="&request("votevalue")
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" style="border-collapse: collapse" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
<tr>
<td colspan="2" bgcolor="#EFEFEF">調查:<%=rs("votetitle")%>(<%=rs("time")%>)</td>
</tr><form name="form1" 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" name="votevalue" 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" name="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" rel="stylesheet" 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" style="border-collapse: collapse" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
<tr>
<td bgcolor="#EFEFEF"><center>
新增新的投票</center></td>
</tr>
<tr>
<td>請輸入這個投票所需要的選項個數。 </td>
</tr>
<tr>
<td><form name="form1" method="post" action="?action=add1">
<select name="num">
<%for i1=2 至 10%>
<option value="<%=i1%>"><%=i1%></option>
<%next%>
</select>
<input type="submit" name="Submit" value="Submit">
</form></td>
</tr>
</table>
<%
end if
if action="add1" then
num=request("num")
%>
<table border=1 cellspacing="0" style="border-collapse: collapse" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
<tr>
<td bgcolor="#EFEFEF"><center>
新增新的投票</center></td>
</tr>
<tr>
<td>請輸入這個投票所需的詳細資料。 </td>
</tr>
<tr>
<td><form name="form2" method="post" action="?action=add2">
投票名稱
<input type="text" name="votetitle">
<br>
<%for i2=1 to num%>
選項<%=i2%><input type="text" name="<%=i2%>"><br>
<%next%>
<input type="hidden" name="num" value="<%=request("num")%>">
<input type="submit" name="Submit2" value="Submit">
<input type="reset" name="Submit3" 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" style="border-collapse: collapse" cellpadding="0" bgcolor="#ffffff" bordercolor="#C0C0C0" width=100%>
<tr>
<td colspan="2" bgcolor="#EFEFEF">所有投票清單</td>
</tr>
<%do while not rs.eof%>
<tr>
<td width="44%">編號:<font color="#FF0000"><%=rs("voteid")%> </font><a href="vote.asp?voteid=<% =rs("voteid")%>" target="_blank"><%=rs("votetitle")%>
</a>(<%=rs("time")%>)</td>
<td width="50%">【<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%>
最後顯示效果如下: