从数据库中随机选择

fdx2calv  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(324)

我正在构建一个测验应用程序。我想随机抽取问题,效果很好。但问题在于答案。我试着获取每个问题的id并将其附加到每个问题上。其本质是用问题id得到正确答案,但我只得到一个值,而不是所有已回答的问题。我添加了一个隐藏的输入类型,它保留了问题id,以便使用它来获取每个问题的正确答案。
这是问题查询

<form method="POST" role="form" id="form" action="result.php">
    <?php
      // fetch out questions and answers from the database
      $qryquestions="SELECT * FROM questions WHERE `course_title`='".$course_title."' ORDER BY RAND() LIMIT 10";
          $qryquestionscheck=$conn->query($qryquestions);
          foreach ($qryquestionscheck as $row){
            $question_id = $row['id'];
            $questions = $row['questions'];
            $optionA = $row['option_A'];
            $optionB = $row['option_B'];
            $optionC= $row['option_C'];
            $optionD = $row['option_D'];
            $_SESSION['course_title'] = $course_title;

         ?>
         <div class="form-group">
            <label style="font-weight: normal; text-align: justify;"><b><?php echo "Question" . " " . $counter++; ?></b>&nbsp<?php echo $questions; ?></label><br>
            <div id="quiz-options">
              <label style="font-weight: normal; cursor: pointer;">
                <input type="checkbox" name="option[]" value="A"> <?php echo $optionA; ?>
              </label><br>
              <label style="font-weight: normal; cursor: pointer;">
                <input type="checkbox" name="option[]" value="B"> <?php echo $optionB; ?>
              </label><br>
              <label style="font-weight: normal; cursor: pointer;">
                <input type="checkbox" name="option[]" value="C"> <?php echo $optionC; ?>
              </label><br>
              <label style="font-weight: normal; cursor: pointer;">
                <input type="checkbox" name="option[]" value="D"> <?php echo $optionD; ?>
              </label><br>
              <input type="hidden" name="question_id[]" value="<?php echo $question_id; ?>">
            </div>
            <hr>
         </div><br>
         <?php } ?>
        <input class="btn btn-primary pull-right"  name="submit" type="submit" value="Submit">
        </form>

在这里我用每个问题的id找出正确的答案

//check and compare anwsers
if (isset($_POST['submit'])){

    $option_array = $_POST['option'];
    $each_question_id = $_POST["question_id"];
    echo json_encode($each_question_id).'<br/>';
    // convert question id to string
    $each_question_id_string = implode(",", $each_question_id);
    echo $each_question_id_string . '<br>';

    if (empty($option_array) == false){
      //select answer with the question ID
      $query = "SELECT answer FROM answers WHERE `question_id`='".$each_question_id_string."'";
      $checkquery = $conn->query($query);
      $correct_option = array();

      while ($answerrows = $checkquery->fetch_assoc()){
        $correct_option[] = $answerrows['answer'];
      }

      $correct_string = implode(",", $correct_option);
      echo $correct_string. '<br>';

      $correct_array = explode(",", $correct_string);
      echo json_encode($correct_array).'<br/>';
      echo $each_question_id_string.'<br/>';
      exit();

      $result= array_intersect_assoc($correct_array,$option_array);
      $resultcount = count($result);
      $noOfQuestions = "10";
      $wrongAnswers = $noOfQuestions - $resultcount;

      $date_taken = date('Y-m-d:h:i:s');

      // performance
      if ($resultcount >= "7"){
        $performance = "Excellent";
      } else if ($resultcount >= "5") {
        $performance = "Good";
      } else if ($resultcount <= "4") {
        $performance = "Poor";
      }

      $insertresult = "INSERT INTO result (`username`, `fullname`, `result`, `matricNo`, `date_taken`, `course_title`) VALUES ('$username', '$fullname', '$resultcount', '$matricNo', '$date_taken', '$course_title')";
      $checkinsert = $conn->query($insertresult);
      if (!$checkinsert){
        die ('Error inserting has occurred');
      }
    } else {
      ?><script type="text/javascript">
        alert('You need to attempt at least one question');
        window.location = "select_course.php";
      </script><?php
    }
    /*?><script type="text/javascript">
     else {
      console.log('Cancel');
     }</script><?php*/
  } else {
    echo "<p style='text-align: center; font-size: 18px;'>Your Quiz     session has expired... Click <a href='user_dashboard.php'>here</a> to go to your dashboard and re-take the exam if needed</p>";
  }

现在的问题是只有一个答案正在被提取。请帮帮我

dw1jzc5e

dw1jzc5e1#

问题解决了。我试着一次随机抽取问题和答案,不需要再次使用第二个查询

相关问题