从数据库中随机选择

时间:2018-10-04 09:42:18

标签: php mysql arrays

我正在构建测验应用程序。我想获取随机获取的问题,这很好。但是问题出在答案上。我试图获取每个问题的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>";
  }

现在的问题是只有一个答案可以得到。请帮帮我

1 个答案:

答案 0 :(得分:0)

问题解决了。我尝试一次随机获取问题和答案,而无需再次使用第二次查询

相关问题