daily pastebin goal
47%
SHARE
TWEET

Untitled

a guest Sep 14th, 2018 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. php quiz displaying number of correct and number of wrong answers
  2. <p>1. Is Manhattan near New York?<br>
  3.     <input type="radio" name="ans1" value="3">
  4.     Yes<br>
  5.     <input type="radio" name="ans1" value="2">
  6.     Maybe<br>
  7.     <input type="radio" name="ans1" value="1">
  8.     No</p>
  9.   <p>2. Do you like Indian food?<br>
  10.     <input type="radio" name="ans2" value="1">
  11.     Some times<br>
  12.     <input type="radio" name="ans2" value="3">
  13.     Never<br>
  14.     <input type="radio" name="ans2" value="2">
  15.     Always</p>
  16.    
  17. if($ans1 == "3") {
  18. $test_complete .="Question one is <span class='green'>correct</span>, well done!<br/>";  
  19. }else{
  20. $test_complete .="Question one is <span class='red'>incorrect</span>!<br/>";
  21. }
  22. // change the quest2 to the right answer
  23. if($ans2 == "2") {
  24. $test_complete .="Question two is <span class='green'>correct</span>, well done!<br/>";
  25. }else{
  26. $test_complete .="Question two is <span class='red'>incorrect</span>!<br/>";
  27. }
  28.    
  29. $result = $ans1 == 3;
  30. $result += $ans2 == 2;
  31. $result += $ans3 == 1;
  32.  
  33. echo "You got $result/3 questions right";
  34.    
  35. $correct_answers = array('3', '2');
  36. for ($i = 0; $i < $number_of_questions; $i++)
  37. {
  38.    $ans_var = 'ans'.$i;
  39.    if ($_POST[$ans_var] == $correct_answers[$i])
  40.    {
  41.       $score++;
  42.    }
  43. }
  44.    
  45. session_start();
  46. if (!isset($_SESSION['total'])){$_SESSION['total']=0;}
  47. if (!isset($_SESSION['correct'])){$_SESSION['correct']=0;}
  48. if($ans1 == "3") {$_SESSION['correct']+=1;}
  49. if($ans2 == "2") {$_SESSION['correct']+=1;}
  50. if (isset($ans1) || isset($ans2)) {$_SESSION['total']+=1}
  51. echo '<table><tr><td>Correct answers:</td><td>'.$_SESSION['correct'].'</td></tr>';
  52. echo '<tr><td>Wrong answers:</td><td>'.$_SESSION['total']-$_SESSION['correct'].'</td></tr>';
  53. echo '<tr><td>Total answers</td><td>'.$_SESSION['total'].'</td></tr></table>';
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top