0

I am making a quiz application where I have a multiple choice question in PHP. The user gets a question and multiple choices to select the answers. The options what they get will refresh (shuffle/ rand func) everytime. So when the user selects the checkbox and the options gets verified with the correct answer and score them. E.g. if there are 3 right choices and user selects all 3 correct they get 5 points and partial correct they get 3 points and for wrong selections they get 0. Please find my code below. Thanks for your time,

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="css/style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<nav class="navbar navbar-inverse"></nav>
<div class="container-fluid text-center">
  <div class="row content">
    <div class="col-sm-2 sidenav">
      <p>&nbsp;</p>
    </div>
    <div class="col-sm-8 text-left">
      <h1>Question 1</h1>
      <p>Multiple choice question</p>
      <div class="row"> </div>
      <hr>
      <p>Which of the following cities are in India?</p>
      <?php
$a=array("London","Mumbai","Berlin","Tokyo","Delhi", "Patna" , "Lahore");
$random_keys=array_rand($a,7);
shuffle($a);?>
      <div class="form-check">
        <form class="form-inline" action="#" method="post">
          <div class="checkbox">
            <label class="form-check-label">
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[0]]?>">
              <?php echo $a[$random_keys[0]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[1]]?>">
              <?php echo $a[$random_keys[1]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[2]]?>">
              <?php echo $a[$random_keys[2]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[3]]?>">
              <?php echo $a[$random_keys[3]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[4]]?>">
              <?php echo $a[$random_keys[4]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[5]]?>">
              <?php echo $a[$random_keys[5]]."<br>";?>
              <input class="form-check-input" type="checkbox" name="check_list[]"  value="<?php echo $a[$random_keys[6]]?>">
              <?php echo $a[$random_keys[6]]."<br>";?> </label>
          </div>
          <div class="form-group">
            <button type="submit" class="btn btn-default" name="submit">Submit</button>
          </div>
        </form>
      <?php
      $totalscore = 0;
            if(isset($_POST["submit"])){

            if (!empty($_POST["check_list"])){
            echo "<p>  You Selected: </p>";

            foreach ($_POST["check_list"] as $answers)


                echo "<p>".$answers ."</p>";

            }

            else{

            echo "Please make atleast one selection.";  
            }
            }


?>


      </div>
    </div>
    <br>
    <br>
  </div>
</div>
</div>
<footer class="container-fluid text-center">
  <p>SM (c) 2018</p>
</footer>
</body>
</html>
3
  • well you haven't setup any answers to check against? Have 2 seperate arrays such as correctAnswers and incorrectAnswers, Pull out random values from both and merge these into a single array. Then all you have to do is loop through each $_POST["check_list"] and use in_array() on the looped value inside the correct answer array adding to the total score if correct. Commented Mar 22, 2018 at 23:06
  • thanks @Juakali92 can u please help me. I am not sure and I am I new to coding. Thanks Commented Mar 22, 2018 at 23:18
  • is this homework of somekind? If so i think it would be better if you had a go with what i've suggested and if you get stuck, i'll help you from there Commented Mar 22, 2018 at 23:25

1 Answer 1

1

As juakali92 said you could use two arrays. What you could do is to make another array which contains right answers. Then compare array containing user answers to correct answers array with array_diff() function and find count of wrong answers. Use count() to get count of right answers and max count of right answers. Then compare/do what ever you want to with that data.

Here's an example:

<?php

$totalscore = 0;

//  All answers
$a = array("london", "mumbai", "berlin", "tokyo", "delhi", "patna", "lahore");
$rnd = array_rand($a, 7);
shuffle($a);

//  Correct answers for same questions
$c = array("mumbai", "delhi", "patna");

//  Check answer
if (isSet($_POST['submit'])) {

    $user_answerArr = $_POST['check_list'];

    //  Get count of wrong answers
    $wrongCount = count(array_diff($c, $user_answerArr));

    //  Max points for right answers
    $maxPoints = count($c);

    //  For this example each right answer gives 1 point
    $totalscore += $maxPoints - $wrongCount;

    echo "Score: " . $totalscore;
}

?>

<form method="post">
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[0]]; ?>"/> <?php echo $a[$rnd[0]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[1]]; ?>"/> <?php echo $a[$rnd[1]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[2]]; ?>"/> <?php echo $a[$rnd[2]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[3]]; ?>"/> <?php echo $a[$rnd[3]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[4]]; ?>"/> <?php echo $a[$rnd[4]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[5]]; ?>"/> <?php echo $a[$rnd[5]] ?> <br>
    <input type="checkbox" name="check_list[]" value="<?php echo $a[$rnd[6]]; ?>"/> <?php echo $a[$rnd[6]] ?> <br>
    <button type="submit" name="submit">Submit</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.