smithson123 Posted September 28, 2009 Share Posted September 28, 2009 I am pretty much a novice and am having problems with the php script below. In the showScore function, question 4 is worth 2 marks with there being 2 correct answers out of a list of 4. I've got as far as below and now i'm a bit stuck. When i choose different answers for question 4 the scoring system does not work correctly. No doubt what I have done below is incorrect anyway but any help would be much appreciated Thanks <?php $name = $_POST['userName']; $email = $_POST['userEmail']; $question1 = $_POST['Q1']; $question2a = $_POST['Q2a']; $question2b = $_POST['Q2b']; $question2c = $_POST['Q2c']; $question2d = $_POST['Q2d']; $question3 = $_POST['Q3']; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Quiz Results</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <h1>Quiz Results</h1> <?php showFormInput(); ?> <p>Question 4: <?php $question4 = $_POST['Q4']; foreach ($question4 as $Q4) { echo "$Q4 , "; } ?> </p> <?php showScore(); ?> <p>To retake the quiz click <a href="quiz.html">here</a></p> <p>Submitted answers are stored in the <a href="quizResults.txt">quizResults</a> document</p> <?php logResults("store"); ?> </body> </html> <?php function showFormInput() { global $name, $email, $question1, $question2a, $question2b, $question2c, $question2d, $question3; Print <<<END <p>Your Name: $name</p> <p>Your Email: $email</p> <p>Your answers were</p> <p>Question 1: $question1</p> <p>Question 2: $question2a $question2b $question2c $question2d</p> <p>Question 3: $question3</p> END; } function showScore() { global $question1, $question2a, $question2b, $question2c, $question2d, $question3, $Q4, $score; $score == 0; if ($question1=="Apache") $score=$score+1; if ($question2a=="HTML") $score=$score+1; if ($question2b=="ECMA") $score=$score+0; if ($question2c=="WML") $score=$score+0; if ($question2d=="DOM") $score=$score+1; if ($question3=="Flexible") $score=$score+1; if ($Q4=="H1" ||$Q4=="DIV") ($score=$score+2); echo "You scored $score out of 6"; } function logResults($action) { global $name, $email, $question1, $question2a, $question2b, $question2c, $question2d, $question3, $Q4, $score; $logEntry = <<< END ---------------- Name: $name E-Mail: $email Question 1: $question1 Question 2: $question2a $question2b $question2c $question2d Question 3: $question3 Question 4: $Q4 Total Score: $score END; if ($action == "email") { return $logEntry; } else { $filename = "surveyLog.txt"; $fp = fopen($filename, "a+") or die ("Couldn't open $filename"); fwrite($fp,$logEntry); fclose($fp); } } ?> Link to comment https://forums.phpfreaks.com/topic/175842-problems-with-quiz-scoring-script/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.