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

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站