duanchao4445 2018-03-22 22:43
浏览 54
已采纳

PHP:如何检查复选框的值并与数组进行比较?

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>
  • 写回答

1条回答 默认 最新

  • dtxw20878 2018-03-22 23:51
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!
  • ¥15 (标签-python)
  • ¥20 搭建awx,试了很多版本都有错
  • ¥15 java corba的客户端该如何指定使用本地某个固定IP去连接服务端?
  • ¥15 activiti工作流问题,求解答
  • ¥15 有人写过RPA后台管理系统么?
  • ¥15 Bioage计算生物学年龄
  • ¥20 如何将FPGA Alveo U50恢复原来出厂设置哇?
  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?