您的位置:首页精文荟萃软件资讯 → 创建一个Web投票系统

创建一个Web投票系统

时间:2004/10/7 19:09:00来源:本站整理作者:蓝点我要评论(0)


            
             
              
             
            

               
               

            



            下面zip文件:http://www.content.aspdir.co.uk/files/Article-11.zip

During this article you will learn how to construct your own web poll using ASP. The article presumes you
already understand basic database interaction.


The following samples of code allow a user to select one of four options to a question. The user's vote is
then recorded in a database and the user is taken to a page where the results for each option can be
viewed in statistical, numerical and graphical form. Not bad, huh?

The whole application is based on the database itself, so when the values within the database are altered,
the application automatically changes. The database design itself, is not included within this article so
make sure to download a copy of the entire application, including the database before running the code.

The code for the first page is shown as follows: -

Page: default.asp

<%
'Connects to database using recordset method
Function dataConn(database,connection,recordset)
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
connection.Open "DBQ=" & Server.Mappath(database) & ";Driver={Microsoft Access Driver (*.mdb)};"
End Function
%>


Poll Example




What is your favoutrite language?


<%
'Calls dataConn function to open dbPoll.mdb
dataConn "files/dbPoll.mdb",POdc,LArs

'Selects all fields within tblLanguages
LArs.Open "SELECT * FROM tblLanguages", POdc

'Loop through and display each record within the database as a radio button
Do While Not LArs.EOF
Response.Write LArs("Language") & ":
"
LArs.MoveNext
Loop

'Close database connection
LArs.Close
POdc.Close
Set POdc = Nothing
%>
View Poll






Once connected to the database the script loops through each record, and displays that option as a radio
button. When the 'Vote' button is pressed, the individual value of the selected radio button is submitted
to the next page.

The code for the next page is shown below: -

Page: pollResults.asp

<%
'Define all variables that will be used
Dim I, Percent

'Connects to database using recordset method
Function dataConn(database,connection,recordset)
Set connection = Server.CreateObject("ADODB.Connection")
Set recordset = Server.CreateObject("ADODB.Recordset")
connection.Open "DBQ=" & Server.Mappath(database) & ";Driver={Microsoft Access Driver (*.mdb)};"
End Function
%>


Polling Sample



<%
'Calls dataConn function to open dbPoll.mdb
dataConn "files/dbPoll.mdb",POdc,LArs

'Selects all fields within tblLanguages
LArs.Open "SELECT * FROM tblLanguages", POdc,1,2

'Loop through and total up number of votes for all records
Do While Not LArs.EOF

'If record contains voted language then increment votes
If LArs("Language") = Request.Form("language") Then
LArs("Votes") = LArs("Votes") + 1
LArs.Update
End If
I = I + LArs("Votes")
LArs.MoveNext
Loop

'Calculate value which will be used to calculate percentage
Percent = 100 / I
LArs.MoveFirst

'Loop through and recalculate percentage of votes for each record
Do While Not LArs.EOF
LArs("Percentage") = LArs("Votes")*Percent
LArs.Update
LArs.MoveNext
Loop
LArs.Close

'Selects all fields within tblLanguages
LArs.Open "SELECT * FROM tblLanguages ORDER BY Percentage DESC", POdc

'Loop through and display all updated records
Do While Not LArs.EOF
Response.Write "" & LArs("Language") & " (Votes: " & LArs("Votes") & ")"

'Set pixel-width of table cells to percentage number
Response.Write"bgcolor=" & LArs("BarColour") & ">"
Response.Write"
" & LArs("Percentage") & " %
"
LArs.MoveNext
Loop

'Close database connections
LArs.Close
POdc.Close
Set POdc = Nothing
%>





The second page first retrieves the selected option from the form submitted by the user. The page then
loops through each record and calculates the total number of votes in order to find out what number of
votes is equal to 1%. When the page finds the record that the user submitted, presuming a value was
submitted at all, the number of votes for that option is incremented.

Using the value which determines how many votes are equal to 1%, we again loop through each record and re-
calculate the percentage of votes each option has received by multiplying this previously determined value
by the number of votes that option has received. It's all 10 y/o maths stuff really, so be very ashamed of
yourself if you're finding this difficult, :P.

Once the records have been updated, all that is left to do is simply loop through each record in
descending order of their percentage.

The small bar underneath each option is just a table cell with it's pixel-width determined by the
percentage of that particular option. All in all it's pretty simple stuff, just takes a bit of work to fix
it all together.

Have a Nice Day,
sTePHeN ;)

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3

最新文章 微信3.6.0测试版更新了微信支付漏洞会造成哪 360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗

人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本