简体   繁体   English

从数据库中提取具有多个答案的调查

[英]Pulling a survey with multiple answers from a database

I have a working survey that pulls each question and any related answers from multiple tables. 我有一份工作调查,可以从多个表格中提取每个问题和任何相关的答案。 Each question is stored in the following table: 每个问题存储在下表中:

  • tblQuestions, with the fields: tblQuestions,具有以下字段:
  • qID qID
  • qText. qText。

The related answers are stored in the table: 相关答案存储在表中:

  • tblPossAnswers, with the fields: tblPossAnswers,具有以下字段:
  • aID 援助
  • qID qID
  • answerText. answerText。

So, I would have 3 possible answers for each question. 因此,对于每个问题,我都会有3个可能的答案。 My sql to pull everything is: 我的SQL拉一切:

select * from tblQuestions, tblPossAnswers where
tblPossAnswers.qID = tblQuestions.qID
order by tblQuestions.qID ASC

And my PHP to display it: 和我的PHP来显示它:

while ($row = mysql_fetch_array($result)) {

echo "<p>" . $row['qText'] . "</p><br />";
echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
echo $row['answerText'];
}

The problem is this is displaying the qText every time it displays a possible answer. 问题是这是在每次显示可能的答案时都显示qText。 So it looks like: 所以看起来像:

  • Question 1 问题1
  • Possible answer 1 可能的答案1
  • Question 1 问题1
  • Possible answer 2 可能的答案2
  • Question 1 问题1
  • Possible answer 3 可能的答案3

  • Question 2 问题2
  • Possible answer 1 可能的答案1
  • Question 2 问题2
  • Possible answer 2 可能的答案2
  • Question 2 问题2
  • Possible answer 3 可能的答案3

What I would like to do is have the qText only display when the first possible answer is pulled. 我想做的是仅在提取第一个可能的答案时才显示qText。 I'm still somewhat of a newb to MySQL, so the solution might be something very simple that I'm just not seeing. 我仍然对MySQL还是有点陌生​​,所以解决方案可能很简单,我只是没看到。

You can either test for whether the question has changed within your PHP loop: 您可以测试问题是否在您的PHP循环中发生了变化:

while ($row = mysql_fetch_array($result)) {
  if ($row['qID'] != $lastQuestionID) {
    echo "<p>" . $row['qText'] . "</p><br />";
    $lastQuestionID = $row['qID'];
  }
  echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />";
  echo $row['answerText'];
}

Or else you can group the MySQL results by question using GROUP_CONCAT , specifying a separator on which you then split the answers in PHP: 否则,您可以使用GROUP_CONCAT将MySQL结果按问题分组,并指定一个分隔符,然后在该分隔符上拆分PHP中的答案:

select *, group_concat(answerText separator char(30)) as answers
from tblQuestions join tblPossAnswers using (qID)
group by tblQuestions.qID
order by tblQuestions.qID ASC

Then: 然后:

while ($row = mysql_fetch_array($result)) {
  echo "<p>" . $row['qText'] . "</p><br />";
  foreach (explode(chr(30), $row['answers']) as $answer) {
    echo "<input type='radio' name='".$row['qID']."' value='".$answer."' />";
    echo $answer;
  }
}

No need to change anything in sql just change php slightly as following 无需更改sql中的任何内容,只需将php稍作更改,如下所示

while ($row = mysql_fetch_array($result)) {
  if ($row['qText'] != $lastQuestion) {
    echo "<p>" . $row['qText'] . "</p><br />";
    $lastQuestion = $row['qText'];
  }
  echo "<input type='radio' name='".$row['qID']."' value='".$row['aID']."' />".$row['answerText'];

}

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

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