网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP实例
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.通过ASP与ACCESS数据库建立连接(.
.用ASP实现对MP3曲目信息的操作.
.利用ASP存取各种常用类型数据库(.
.下拉框连动的小例子(.htm版).
.实例演练ASP+XML编程(3).
.jmail4.1用pop3收信的例子.
.使用 ASP+ 列表绑定控件(中).
.Jmail发信的实例,模块化随时调用.
.一个用asp+存取数据库的例子.
.如何用ASP编写网站统计系统(一).
.用ASP、VB和XML建立互联网应用程.
.用ASP技术编制隐藏用户密码程序.
.ASP网站远程客户实现EXCEL打印功.
.实 现 滚 动 新 闻.
.用asp.net写的论坛程序--论坛主页.
.一个简单的网上书城的例子(六).
.asp提供在线文章翻译的功能.
.ASP进阶之文章在线管理更新(7).
.利用ASP在客户端注册DLL文件.
.用EasyMailObject组件处理Exchan.

创建一个Web投票系统

发表日期:2001-8-22


下面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 ;)

上一篇:使用JScript.NET创建asp.net页面(七) 人气:11113
下一篇:W3 Jmail中文使用说明 人气:15394
浏览全部投票的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐