欢迎光临
我们一直在努力

创建一个Web投票系统

建站超值云服务器,限时71元/月

下面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 users 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

%>

<html>

<head>

<title>poll example</title>

</head>

<body>

<form name="languages" method=post action="pollresults.asp">

<p>what is your favoutrite language?</p>

<%

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") & ": <input type=radio name=language value=" & lars("language")

& "><br>"

lars.movenext

loop

close database connection

lars.close

podc.close

set podc = nothing

%>

<a href="pollresults.asp">view poll</a>

<input type=submit value="vote">

</form>

</body>

</html>

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

%>

<html>

<head>

<title>polling sample</title>

</head>

<body>

<%

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 "<b>" & lars("language") & "</b><i> (votes: " & lars("votes") & ")</i>"

set pixel-width of table cells to percentage number

response.write"<table cellpadding=0 cellspacing=0 height=10 width=" & lars("percentage") & "

bgcolor=" & lars("barcolour") & ">"

response.write"<tr><td></table>" & lars("percentage") & " %<br>"

lars.movenext

loop

close database connections

lars.close

podc.close

set podc = nothing

%>

</body>

</html>

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. its all 10 y/o maths stuff really, so be very ashamed of

yourself if youre 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 its pixel-width determined by the

percentage of that particular option. all in all its pretty simple stuff, just takes a bit of work to fix

it all together.

have a nice day,

stephen 😉

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 创建一个Web投票系统
分享到: 更多 (0)