帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
创建一个Web投票系统
作者:Timer(转) 发布时间:2005-03-12 来源:因特耐特
下面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
%>
<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. 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 ;)

  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·用VB创建FTP组件(put)  (2005-03-12)
 ·用VB创建FTP组件(get)  (2005-03-12)
 ·在不支持FSO的服务器上使用XMLDO  (2005-03-12)
 ·使用Java Swing 创建一个XML编辑  (2005-03-12)
 ·使用Java Swing 创建一个XML编辑  (2005-03-12)
 ·使用Java Swing 创建一个XML编辑  (2005-03-12)
 ·用PHP创建动态图形  (2005-03-11)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统