<%Option Explicit%> <% Function WriteToFile(destFile, lsResults) Dim filename, oFile, oDestination filename = server.mappath(destFile) Set oFile = CreateObject("Scripting.FileSystemObject") Set oDestination = oFile.OpenTextFile(filename, 2, True) oDestination.Write lsResults End Function Function ReadFile(srcFile) Dim filename, oFile, oSource filename = server.mappath(srcFile) Set oFile = CreateObject("Scripting.FileSystemObject") Set oSource = oFile.OpenTextFile(filename, 1, False) ReadFile = oSource.ReadAll oSource.Close Set oFile = Nothing Set oSource = Nothing End Function Function WriteResponse(Value, AnswerCount) Dim lsResultString laResults(Value - 1) = laResults(Value - 1) + 1 For i = 1 to AnswerCount If lsResultString = "" Then lsResultString = "a" & cstr(i) & "=" & laResults(i - 1) Else lsResultString = lsResultString & "&a" & cstr(i) & "=" & laResults(i - 1) End If Next Call WriteToFile("results.txt", lsResultString) End Function '********************************************************** '***** Dynamic User-Poll - Written by Jei Gaither ***** '********************************************************** '// This script reads in Question, Answer, and answer '// totals from the files survey.txt and results.txt. '// This could be implemented using PHP, JSP, Perl or '// any other CGI as long as the return string is '// formatted as URL-Encoded name/value pairs. Dim lsReturnString, lsQuestionText ', lsAnswer1Text, lsAnswer2Text, lsAnswer3Text, lsAnswer4Text, lsAnswer5Text, lsAnswer6Text, lsAnswer7Text Dim liResponses, liAnswers, i, laAnswers(), laResults(), lsResultString Dim liResponse '// Save response in local variable if it exists '// The value being passed back will be in the format btnAnswer# '// where # is the number of the answer they've chosen (starting w/ 1). '// This should probably be done w/in Flash instead to make it easier '// for processing. Use the SubString command in your Flash adaptation. If Request("lsResponse") <> "" Then liResponse = cint(Trim(mid(Request("lsResponse"),10,1))) Else liResponse = 0 End If 'Read in questions and answers Dim lsQAString, liQStart, liQEnd, liAStart, liAEnd, liNextAnswer 'Read the Q&As into a single string lsQAString = ReadFile("survey2.txt") '// The following scheme for reading in Question and Answer '// values assumes that the layout of the file is: '// q1=Question&a1=First+Answer&a2=Second+Answer ... '// i.e. the Question must be before the answers. 'Get Number of Answers liAnswers = mid(lsQAString, (inStr(1, lsQAString, "&answers=") + 9)) ReDim laAnswers(liAnswers) ReDim laResults(liAnswers) 'Find Start and End of Question string liQStart = instr(1, lsQAString, "q1=") + 3 liQEnd = instr(1, lsQAString, "&a1=") 'Store Question text into lsQuestionText lsQuestionText = mid(lsQAString, liQStart, liQEnd - liQStart) 'Store Answer values into laAnswer() array For i = 1 to liAnswers liAStart = instr(1, lsQAString, "&a" & cstr(i)) + 4 liAEnd = (instr(liAStart, lsQAString, "&a")) ' & cstr(i + 1) & "=") - 2) laAnswers(i - 1) = mid(lsQAString, liAStart, (liAEnd - liAStart)) Next lsReturnString = "q1=" & server.URLEncode(lsQuestionText) For i = 1 to liAnswers lsReturnString = lsReturnString & "&atext" & i & "=" & server.URLEncode(laAnswers(i - 1)) Next lsReturnString = lsReturnString & "&cntrAnswers=" & liAnswers If liResponse > 0 Then 'User has selected an answer lsResultString = ReadFile("results.txt") For i = 1 to liAnswers liAStart = instr(1, lsResultString, "&a" & cstr(i)) + 4 liAEnd = (instr(liAStart, lsResultString, "&a")) If liAEnd > 0 Then laResults(i - 1) = mid(lsResultString, liAStart, (liAEnd - liAStart)) Else laResults(i - 1) = mid(lsResultString, liAStart) End If Next 'Get the response total For i = 1 to liAnswers liResponses = liResponses + cint(laResults(i - 1)) Next 'Save Answer if user hasn't answered yet If Session("answer") = "" Then Call WriteResponse(liResponse, liAnswers) Session("answer") = liResponse 'cint(mid(Request("lsResponse"),10)) End If 'Add Percentages to result string for i = 1 to liAnswers lsReturnString = lsReturnString & "&a" & cstr(i) & "pct=" & Server.URLEncode(FormatNumber((laResults(i - 1) / liResponses),4)*100) & "&val" & cstr(i) & "=" & Round(laResults(i - 1) / liResponses,2) * 100 Next End If Response.Write(lsReturnString) %>