續上篇 前面我們已經完成了文件設計,并寫了surveycode.asp的代碼,F在我們來完成 其它的文件。在完成這些文件的時候,別忘了前面寫的程序流程,如果記不清,最好打印 一份出來對著看。
一、統計結果 首先我們來完成與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> </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>
<% 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 %>
<% closetable rs '關閉表 closedb my '關閉數據庫 end sub '---------------------- %> </body> </html>
到這里,我們已經完成了顯示統計結果和列出所有調查的兩個文件,最后只剩下 管理頁面了,下一篇我們將來完成管理頁面。
|