我想向用户显示未答复的调查问题。如果他已经回答了这个问题,我想展示下一个未回答的问题。
<?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
猫不应该在那里。这是上一个问题的答案之一。我究竟做错了什么?
答案 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>