如果表中已找到数据,如何显示下一个数据集?

时间:2017-04-26 03:21:26

标签: php mysqli

我想向用户显示未答复的调查问题。如果他已经回答了这个问题,我想展示下一个未回答的问题。

            <?php 
             $i=1; 
             $surveyQ = "SELECT * FROM ve_survey_answers a 
             INNER JOIN ve_survey_questions q 
             ON a.QuestionId = q.id
             ORDER BY a.QuestionId
             LIMIT 4";
             $surveyResult = mysqli_query($db, $surveyQ);
             $question = "";
             while ($survey=mysqli_fetch_array($surveyResult)){$i++;
             $curr_question = $survey['Question'];
             $curr_questionid = $survey['QuestionId'];
             //check if user has already answered question
             $answeredquestions=mysqli_query($db, "SELECT QuestionId FROM ve_survey_useranswers a JOIN ve_users u ON a.UserId=u.UserID WHERE a.QuestionId='$curr_questionid' AND u.ProfileName = '$user_check'");
             if ($question != $curr_question && $answeredquestions->num_rows == 0) {
             $question = $curr_question;
             echo "<h5 style='font-weight: bold;'>$question</h5>";
             }
             elseif($question != $curr_question && $answeredquestions->num_rows > 0) {
             //display an unanswered question
             $surveyQ = "SELECT * FROM ve_survey_answers a 
             INNER JOIN ve_survey_questions q 
             ON a.QuestionId = q.id
             WHERE q.id <> $curr_questionid
             ORDER BY a.QuestionId LIMIT 4";
             $surveyResult = mysqli_query($db, $surveyQ);
             }
             ;?>
    <div class="div">
    <input type="radio" name="useranswer" id="radio<?=$i;?>" class="radio" 
    value='<?=$survey['AnswerId'];?>'/>

    <label class="surveylabel" for="radio<?=$i;?>"><?=$survey['Answer'];?>
    </label>
    </div>
   <?php } ;?>

我写的代码显示如下:

Cat// data from the previous question
If you could reincarnate into an animal, which animal would it be?
Sloth
Tiger
Meerkat
Blue Whale

猫不应该在那里。这是上一个问题的答案之一。我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

我更改了代码并且有效:

        <?php 
        $i=1; 
        //check user answers
         $surveyanswerQ = "SELECT * FROM ve_survey_useranswers ORDER BY QuestionId";
         $surveyanswerResult = mysqli_query($db, $surveyanswerQ);
         $surveyanswer=  mysqli_fetch_assoc($surveyanswerResult);
         //select all questions
        $surveyQ = "SELECT * FROM ve_survey_questions q JOIN ve_survey_answers a ON q.id=a.QuestionId WHERE q.id <> {$surveyanswer['QuestionId']} ORDER BY a.QuestionId LIMIT 4 ";
         $surveyResult = mysqli_query($db, $surveyQ);
         $question = "";
         while ($survey=mysqli_fetch_array($surveyResult)){$i++;
           $curr_question = $survey['Question'];
         $curr_questionid = $survey['id'];
          if ($question != $curr_question) {
         $question = $curr_question;
         echo "<h5 style='font-weight: bold;'>$question</h5>";
         }
        ;?>
       <div class="div">
       <input type="radio" name="useranswer" id="radio<?=$i;?>" class="radio" value='<?=$survey['AnswerId'];?>'/>
       <label class="surveylabel" for="radio<?=$i;?>"><?=$survey['Answer'];?></label>
       </div>