To read the article online, visit http://www.4GuysFromRolla.com/webtech/071099-1.4.shtml

Creating User Polls, Part 4


  • Read Part 1 of Creating User Polls
  • Read Part 2 of Creating User Polls
  • Read Part 3 of Creating User Polls

    We've reached the end of the road! We are down to showing the results of the poll to the user. Remember, this code exists in the same file as the code that displays the voting options. If you recall, we looked at a cookie at the very beginning to see if the user has voted or not:

    'Determine if this person has already voted today...
    Dim strVoted, strSQL
    strVoted = Request.Cookies("Voted")

    If strVoted contains the value "Yes," then the user has already voted, and we want to show them the results of the poll. So, time for an IF statement!!

    if strVoted = "Yes" then
          strSQL = "SELECT PollOption.PollOptionID, PollOption.QuestionText, PollOption.Votes, " & _
               "Poll.PollQuestion " & _
               "FROM Poll INNER JOIN PollOption ON Poll.PollID = PollOption.PollID " & _
               "WHERE Poll.DateStart <= #" & Date & "# AND Poll.DateEnd >= #" & Date & "#;"

          objRS.Open strSQL, objConn, adOpenStatic, adLockReadOnly

    So, if the user has voted, then we want to get the results of the poll. You'll notice that this SQL string is very familiar to the one that showed the voting options. The only major difference is that we are bringing back the number of Votes for each PollOption row we're interested in. You'll also notice that we are opening this recordset using an adOpenStatic cursor. This is because we will want to know the total number of records in our recordset.

    We now want to create an array the size of the number of records in our recordset. In this array, we'll hold the name and number of votes for each voting option. As we populate this array, we will keep track of the array's size and the total number of votes casted:

    'We need to create a dynamic array just big enough to hold
    ' all of our voting options
    Dim PollOptions()
    Redim PollOptions(objRS.RecordCount,2)

    Dim iCount, iTotalVotes, strPollName
    iCount = 0
    iTotalVotes = 0

    strPollName = CStr(objRS("PollQuestion"))

    Do While Not objRS.EOF
    PollOptions(iCount,1) = CInt(objRS("Votes"))
    PollOptions(iCount,2) = CStr(objRS("QuestionText"))

    iTotalVotes = iTotalVotes + PollOptions(iCount,1)
    iCount = iCount + 1

    objRS.MoveNext
    Loop

    Now that we have this important information, we can work a little magic and display the barcharts:

    <TABLE BORDER=0 CELLSPACING=0 WIDTH=<%=iWidth%>>
    <TR><TH BGCOLOR=NAVY><FONT COLOR=WHITE FACE=Arial SIZE=2>
        <%=strPollName%>
    </FONT></TH></TR>
    <TR>
    <TD BGCOLOR=GRAY>
    <%
    Dim iLoop
    For iLoop = 0 to iCount-1 %>
          <FONT FACE=Arial SIZE=2><%=PollOptions(iLoop,2)%> (<%=FormatPercent(PollOptions(iLoop,1)/iTotalVotes,1)%>)</FONT>
          <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0>
          <TR><TD WIDTH=<%=CInt((PollOptions(iLoop,1)/iTotalVotes)*iWidth)%> BGCOLOR=YELLOW>
           </TD>
          </TR>
          </TABLE>
          <BR>
    <% Next %>

    <FONT FACE=Arial SIZE=2>Total Votes: <%=iTotalVotes%></FONT>
    </TD></TR></TABLE>

    On the right you can see an example of the results of the poll. OK, so the colors may be a tad ugly, but I am no graphics designer! :) Feel free to change those colors around if you like. The neat thing about this is the results are up to the second, since they are all coming from a database.

    Areas for Improvement:

  • To add a new poll you've got to go to the database. A Poll Creation Wizard with a nice front end would be a good improvement to the system. If you write one, please send it my way!
  • Error checking was left out of this code. Several things could happen that could mess this system up. If there is no poll for the current date, then an EOF/BOF error will occur. Also, if there are multiple polls whose date ranges fall within the current date, there's potential to have some bad mojo happen.

    Well, I hope you find this poll system useful! If you have any comments/ideas/suggestions, be sure to send me feedback.


    Attachments::

  • The Poll System, in text format
  • pollResults.asp in text format


  • Read Part 1 of Creating User Polls
  • Read Part 2 of Creating User Polls
  • Read Part 3 of Creating User Polls


  • Article Information
    Article Title: Creating User Polls, Part 4
    Article Author: Scott Mitchell
    Published Date: Saturday, July 10, 1999
    Article URL: http://www.4GuysFromRolla.com/webtech/071099-1.4.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers