home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / polls.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  8.9 KB  |  264 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. ' ADO VBScript constants file
  15. ' You should have a copy of this already, you just need to find it
  16. ' and adjust the include command appropriately!
  17. ' If you can't you can get a copy of this one from:
  18. ' http://www.asp101.com/samples/download/adovbs.inc
  19. %>
  20. <!-- #INCLUDE FILE="./download/adovbs.inc" -->
  21.  
  22. <%
  23. ' You'll notice I'm using an alternate way of creating a DB object
  24. ' as compared to my usual Dim / Create method.  I'm not overly fond
  25. ' of this manner, but there are some advantages to it so I thought I'd
  26. ' introduce those of you who haven't seen it before to this method.
  27. %>
  28.  
  29. <OBJECT RUNAT="server" PROGID="ADODB.Connection" id="cnnPoll"> </OBJECT>
  30. <OBJECT RUNAT="server" PROGID="ADODB.Recordset" id="rsPoll"> </OBJECT>
  31.  
  32. <%
  33. Dim SCRIPT_NAME   ' Script name so we can make it location independent
  34. SCRIPT_NAME = Request.ServerVariables("SCRIPT_NAME")
  35.  
  36. Dim strAction     ' Variable to determine what we need to do
  37. Dim iPoll         ' Poll ID number to perform current action on
  38.  
  39. Dim iVote         ' In the case that a vote is being cast this is the vote
  40.  
  41. Dim iTotalVotes   ' Total votes in DB - used to figure out %s on results page
  42.  
  43. Dim strSQL        ' Our SQL string for getting to the DB
  44. Dim I             ' A standard looping variable
  45.  
  46.  
  47.  
  48. ' Get action choice and format it for easy comparison
  49. strAction = LCase(CStr(Request.QueryString("action")))
  50.  
  51. ' Set to default of showing the question unless results is passed in
  52. ' indicating I should show the results.  This way I don't have to
  53. ' worry about it since strAction is always question or results.
  54. If strAction <> "results" Then strAction = "question"
  55.  
  56.  
  57. ' Get poll id
  58. iPoll = Request.QueryString("pid")
  59.  
  60. ' Validate and refine iPoll
  61. If IsNumeric(iPoll) Then 
  62.     If iPoll > 0 Then
  63.         iPoll = CInt(iPoll)
  64.     Else
  65.         'strAction = "Error: Invalid Poll Id!"
  66.         
  67.         ' I commented out the above and let this slide so you can
  68.         ' just request poll.asp and get back something.
  69.         iPoll = 1
  70.     End If
  71. Else
  72.     ' If it's not numeric I should just error out which I do
  73.     ' but since this script will handle multiple polls I've
  74.     ' set it up to take "all" as a parameter when displaying
  75.     ' results so you can get to see the whole set of results
  76.     ' if you want to.
  77.     If LCase(iPoll) = "all" And strAction = "results" Then
  78.         iPoll = "all"
  79.     Else
  80.         strAction = "Error: Invalid Poll Id!"
  81.     End If
  82. End If
  83.  
  84.  
  85. ' Open our DB connection since every choice hits the DB for something
  86.  
  87. ' This ODBC connection works fine... used until Nov 8, 99
  88. 'cnnPoll.Open "DBQ=" & SERVER.MapPath("polls.mdb") & ";DRIVER={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;"
  89.  
  90. ' Testing OLE DB connection started using it Nov 8, 99
  91. cnnPoll.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & SERVER.MapPath("polls.mdb") & ";"
  92.  
  93. ' Set the default for our SQL string.
  94. ' This is used all the time unless the whole "all" thing comes into play
  95. strSQL = "SELECT * FROM polls WHERE id=" & iPoll & ";"
  96.  
  97. ' Do whatever action is appropriate "question" or "results"
  98. ' Otherwise error out on the else option.
  99. Select Case strAction
  100.     Case "question"
  101.         ' Open our RS to show the choices
  102.         rsPoll.Open strSQL, cnnPoll, adOpenStatic, adLockReadOnly, adCmdText
  103.  
  104.         If Not rsPoll.EOF Then
  105.             ' Show the voting form
  106.             ' You'll need to format this to your liking
  107.             %>
  108.             <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
  109.                 <TR>
  110.                     <TD BGCOLOR="#006600" ALIGN="left"><FONT COLOR="#FFFFFF"><B>Quick Question:</B></FONT></TD>
  111.                     <TD BGCOLOR="#006600">     </TD>
  112.                     <TD BGCOLOR="#006600" ALIGN="right"><A HREF="<%= SCRIPT_NAME %>?action=results&pid=<%= iPoll %>"><FONT COLOR="#FFFFFF" SIZE="-1">View Results</FONT></A></TD>
  113.                 </TR>
  114.                 <TR>
  115.                     <TD COLSPAN="3" BGCOLOR="#009933" ALIGN="center"><FONT COLOR="#FFFFFF"><%= rsPoll.Fields("question").Value %></FONT></TD>
  116.                 </TR>
  117.                 <TR>
  118.                     <TD COLSPAN="3" BGCOLOR="#009933" ALIGN="center">
  119.                         <%
  120.                         ' Loop 1 to 5 since there are only 5 possible choices set up in the DB
  121.                         For I = 1 to 5
  122.                             If Not IsNull(rsPoll.Fields("choice" & I).Value) Then
  123.                                 ' Some spacing if needed
  124.                                 If I <> 1 Then Response.Write "  "
  125.                                 ' Show choices hyperlinked to the vote portion of the script.
  126.                                 %>
  127.                                 <A HREF="<%= SCRIPT_NAME %>?action=results&pid=<%= iPoll %>&vote=<%= I %>"><FONT COLOR="#FFFFFF"><%= rsPoll.Fields("choice" & I).Value %></A></FONT>
  128.                                 <%
  129.                             End If
  130.                         Next 'I
  131.                         %>
  132.                     </TD>
  133.                 </TR>
  134.             </TABLE>
  135.             <%
  136.         Else
  137.             Response.Write "Error: Invalid Poll Id!"
  138.         End If
  139.         
  140.         rsPoll.Close
  141.  
  142.     Case "results"
  143.  
  144.         ' If we're processing a vote then we need to know what it is so:
  145.         ' Get The Vote!
  146.         iVote = Request.QueryString("vote")
  147.  
  148.         ' Validate and refine iVote.  Setting to 0 if invalid!
  149.         If IsNumeric(iVote) Then
  150.             iVote = CInt(iVote)
  151.                             
  152.             If Not(1 <= iVote And iVote <= 5) Then
  153.                 iVote = 0
  154.             End If
  155.         Else
  156.             iVote = 0
  157.         End If
  158.  
  159.         ' If iVote = 0 or iPoll = "all" then I'm just showing the results
  160.         ' Otherwise we need to log them first
  161.         If iVote <> 0 And iPoll <> "all" Then
  162.             ' Log results
  163.         
  164.             ' Open our RS to record the choice
  165.             ' Notice that it's not static or read only.
  166.             rsPoll.Open strSQL, cnnPoll, adOpenKeyset, adLockPessimistic, adCmdText
  167.  
  168.             If Not rsPoll.EOF Then
  169.                 ' Check to be sure they haven't already voted in this session
  170.                 ' This prevents the refresh button from resubmitting the info
  171.                 ' You could make this a lot more sophisticated and useful if
  172.                 ' you had some reason to.  I just don't really care and do it
  173.                 ' mainly for the refresh button issue
  174.                 If Session("AlreadyVoted") <> 1 Then
  175.                     rsPoll.Fields("votes" & iVote).Value = rsPoll.Fields("votes" & iVote).Value + 1
  176.                     rsPoll.Update
  177.  
  178.                     ' Set Flag to prevent revoting
  179.                     Session("AlreadyVoted") = 1
  180.                 End If
  181.  
  182.             Else
  183.                 Response.Write "Error: Invalid Poll Id!"
  184.             End If
  185.  
  186.             rsPoll.Close
  187.         Else
  188.             If iPoll = "all" Then
  189.                 ' Override our standard SQL string to show all otherwise it'll work fine.
  190.                 strSQL = "SELECT * FROM polls ORDER BY id;"
  191.             End If
  192.         End If
  193.  
  194.         ' I've already processed any entry we needed to and set up for all condition.
  195.         ' Time to show the results!
  196.  
  197.         ' Open recordset to show results.
  198.         rsPoll.Open strSQL, cnnPoll, adOpenKeyset, adLockPessimistic, adCmdText
  199.  
  200.         If Not rsPoll.EOF Then
  201.             ' For each poll show results.
  202.             ' Normally just one, but I built it so it'd work for "all" too.
  203.             Do While Not rsPoll.EOF
  204.                 ' Tally the total votes and store it
  205.                 iTotalVotes = rsPoll.Fields("votes1").Value + _
  206.                                 rsPoll.Fields("votes2").Value + _
  207.                                 rsPoll.Fields("votes3").Value + _
  208.                                 rsPoll.Fields("votes4").Value + _
  209.                                 rsPoll.Fields("votes5").Value
  210.  
  211.                 ' Show Results - Format to your liking!
  212.                 %>
  213.                 <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
  214.                     <TR>
  215.                         <TD BGCOLOR="#006600"><FONT COLOR="#FFFFFF"><B>Poll #<%= rsPoll.Fields("id").Value %> Results</B> (based on <%= iTotalVotes %> votes)</FONT></TD>
  216.                     </TR>
  217.                     <TR>
  218.                         <TD BGCOLOR="#009933" ALIGN="center"><FONT COLOR="#FFFFFF"><%= rsPoll.Fields("question").Value %></FONT></TD>
  219.                     </TR>
  220.                     <TR>
  221.                         <TD BGCOLOR="#009933" ALIGN="center">
  222.                             <%
  223.                             ' Loop over choices
  224.                             For I = 1 to 5
  225.                                 If Not IsNull(rsPoll.Fields("choice" & I).Value) Then
  226.                                     ' The math was giving me trouble when I divided 0 by 1 so I avoided the situation
  227.                                     If rsPoll.Fields("votes" & I).Value = 0 Then
  228.                                         %>
  229.                                         <FONT COLOR="#FFFFFF"><%= rsPoll.Fields("choice" & I).Value %>:</FONT> <FONT COLOR="#000000"><B><%= FormatPercent(0, 1) %></B></FONT>  
  230.                                         <%
  231.                                     Else
  232.                                         %>
  233.                                         <FONT COLOR="#FFFFFF"><%= rsPoll.Fields("choice" & I).Value %>:</FONT> <FONT COLOR="#000000"><B><%= FormatPercent((rsPoll.Fields("votes" & I).Value / iTotalVotes), 1) %></B></FONT>  
  234.                                         <%
  235.                                     End If
  236.                                 End If
  237.                             Next 'I
  238.                             %>
  239.                         </TD>
  240.                     </TR>
  241.                 </TABLE>
  242.                 
  243.                 <BR>
  244.                 <%
  245.                 rsPoll.MoveNext
  246.             Loop
  247.         Else
  248.             Response.Write "Error: Invalid Poll Id!"
  249.         End If
  250.  
  251.         rsPoll.Close
  252.  
  253.     Case Else ' "error"
  254.         ' OK so this is pretty lame error handling, but it
  255.         ' catches most stupid things and warns the user.
  256.         Response.Write strAction
  257. End Select
  258.  
  259. cnnPoll.Close
  260.  
  261. ' I can't set the DB objects to nothing because I never created them.
  262. ' This syntax is just weird.
  263. %>
  264.