简体   繁体   English

从数据库中随机选择

[英]Select randomly from the database

I am building a quiz app. 我正在构建测验应用程序。 I want to fetch to fetch the questions randomly which is working fine. 我想获取随机获取的问题,这很好。 But the problem is with the answer. 但是问题出在答案上。 I tried to get each question id and attach it to each question. 我试图获取每个问题的ID,并将其附加到每个问题。 The essence of this is to get the correct answer with the question ID. 这样做的本质是获得带有问题ID的正确答案。 But I am getting only one value instead of all the answered questions. 但是我只得到一种价值,而不是所有回答的问题。 I added a hidden input type that keeps the question ID so as to use it to fetch out each question correct answer. 我添加了一个隐藏的输入类型来保留问题ID,以便使用它来获取每个问题的正确答案。

This is fetch question query 这是获取问题查询

<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>

And this is where I fetch out the correct answers with each question ID 这是我用每个问题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>";
  }

The problem now is only one answer is being fetched . 现在的问题是只有一个答案 Please help me out 请帮帮我

Problem solved. 问题解决了。 I tried to fetch the questions and answers randomly at a time and no need of using second query again 我尝试一次随机获取问题和答案,而无需再次使用第二个查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM