Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Tallying the Results - Page 6

January 8, 2001

Counting Votes Automatically -or- No Manual Recounts Here!

Okay, so we have a Web page with a poll that only allows visitors to vote once. So far so good, now let's get to work on letting people see the results of the poll - why have a poll at all if you cannot see how it turns out?

Remember back near the beginning where we prepared the HTML for the voting system and also kept track of the total number of votes? Well, it is time to revisit that and expand on it. Remember the block of code?

' Extract the options from the database
strSQL = "SELECT * FROM tbl_Responses " & _
	   "WHERE PollID = " & PollID
set rs_Responses = Server.CreateObject("ADODB.Recordset")
rs_Responses.Open strSQL, Conn, 3, 3

'calculate the total number of responses for the poll
'(you will see why now)
Total = 0
While Not rs_Responses.EOF
Total = Total +  rs_Responses("VoteCount")
	PollOptHTML =  PollOptHTML & _
	"<tr><td>" & rs_Responses("PollOption") & _
	"</td><td><input type=""radio""" & _
	"name=""pollopt"" value=""" & _
	rs_Responses("PollOptionID")  & """></td></tr>"
	rs_Responses.MoveNext
Wend
Okay, now we need to add some conditional flow to check for the cookie and then respond as necessary. We will alter the above code so that it calculates the percentage of votes for each option and then outputs them instead of radio buttons. To know whether the user is voting or requesting to view results or not, we will add a link on the HTML that sets a value in the QueryString that we can then test against (called View).
While Not rs_Responses.EOF
   Total = Total +  rs_Responses("VoteCount")
   rs_Responses.MoveNext
Wend

rs_Responses.MoveFirst

While Not rs_Responses.EOF
   If Request.QueryString("View") = "true" then
	pVotes = (rs_Responses("VoteCount") / Total) * 100
	PollOptHTML = PollOptHTML & _
	"<tr><td>" & rs_Responses("PollOption") & _
	"</td><td>" & pVotes & "%</td></tr>"
	rs_Responses.MoveNext
   Else
	PollOptHTML =  PollOptHTML & _
	"<tr><td>" & rs_Responses("PollOption") & _
	"</td><td><input type=""radio""" & _
	"name=""pollopt"" value=""" & _
	rs_Responses("PollOptionID")  & """></td></tr>"
	rs_Responses.MoveNext
   End if
Wend
And we change our HTML to look like:
<html>
<head></head>
<body>
  <form name="poll" action="poll.asp" method="get">
  <table width="250">
 	<tr><td colpan="2">
	<%= PollQuestion %>
	</td></tr>
	<%= PollOptHTML %>
	<tr><td colspan="2">
	<input type="submit" value="Submit Vote">
        <br>
	<a href="poll.asp?View=true">View Results</a>
	</td></tr>
  </table>
  <input type="hidden" name="isSubmitted" value="yes">
  </form>
</body>
</html>
I know that was a huge mouthful of code all at once, but most of it was stuff we have already looked at. As you can see, we split up the creation of PollOptHTML into two different parts of a conditional test. If the link for "View Results" is clicked, then the value of "View" will be true in the QueryString and the code will calculate a percentage for each value based on Total we calculated before (note that we pulled Total out into its own While statement so it could be a complete figure prior to making any calculations based on it). On the other hand if View is not true (which will be the case if the link is not clicked the first time a user hits the page) then PollOptHTML will take its original form - that of a radio buttons ready for input from the user.

Making Cookies - Page 5
Poll Your Visitors with ASP
The Final Code - Page 7


Up to => Home / Authoring / ASP / Polls




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers