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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用