php - 将每个 'StudentAnswer' 字段显示为其关联的每个 'AnswerContent' 字段 (php)

标签 php mysql html arrays if-statement

有两张表,一张是Answer表,一张是StudentAnswer表。我感兴趣的有 6 个字段,Answer 表中有 4 个,StudentAnswer 表中有 2 个。以下是表格及其字段和数据。

Answer Table:

  QuestionId     AnswerId    AnswerContent   AnswerCorrect
   1             1           Leeds                1 (true)
   2             2           Manchester           0 (false)
   3             3           Birmingham           0 (false)

StudentAnswer Table:

    StudentAnswer  QuestionId
        2                1
        3                1
        1                1

(StudentAnswer 字段包含 AnswerId,这些是学生的答案,具体取决于他们为哪个问题选择的答案)

现在这些字段和其他字段存储在一个数组中,并显示在一个表中。 PHP 代码如下:

<table border='1'>
      <tr>
      <th>Session ID</th>
      <th>Question Number</th>
      <th>AnswerContent</th>
      <th>StudentAnswer</th>
      <th>Student ID</th>
      </tr>
      <?php

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

            echo "
      <tr>
      <td>{$row['SessionId']}</td>
      <td>{$row['QuestionNo']}</td>
      <td>{$row['AnswerContent']}</td>
      <td>{$row['StudentAnswer']} </td>
      <td>{$row['StudentId']}</td>
      </tr>";
        }
      ?>

下面显示了它此刻从查询和数组中输出的内容:

Session ID  Question Number Answerid  AnswerContent     StudentAnswer    Student ID       
 ABB          1              1          Leeds                  1           u0867587   
 ABB          1              1          Leeds                  3           u1231231

第 1 行显示学生 u0867587 对问题 1 的回答。正确答案是利兹,当他选择 answerid '1' 时,他的学生答案是利兹,但显然因为 StudentAnswer 是它显示的 int 字段它作为'1'。第 2 行显示学生 u1231231 回答了同样的问题,显然正确答案仍然是利兹,但他选择了伯明翰,这是 answerid '3',所以他的答案是伯明翰,但由于 StudentAnswer 是 int 字段,它显示为 '3' AnswerContent 是链接的到 AnswerId 字段,这样它就知道 AnswerId 的单词答案

下面是我真正想要输出的内容:

Session ID  Question Number   AnswerId AnswerContent     StudentAnswer     Student ID       
     ABB          1               1       Leeds               Leeds            u0867587   
     ABB          1               1       Leeds               Birmingham       u1231231

它的输出与上面的相同,但不是将它显示为来自 StudentAnswer 字段的 int,而是使用 AnswerContent 将其显示为单词答案。所以 AnswerContent 会自动链接到 Answerid,但我也想将它与 StudentAnswer 链接。

StudentAnswer 字段与 AnswerId 相同,因为它根据学生选择的答案检索 AnswerId。

我确实尝试了 $row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent']; 但它没有用。如何显示每个 StudentAnswer 的 Answercontent?

查询如下:

SELECT 
    SessionId,
    q.QuestionNumber,
    a.AnswerContent,
    a2.AnswerContent as StudentAnswerContent,
    StudentId
FROM Question q
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
    JOIN Answer a ON sa.QuestionId = a.QuestionId  
    JOIN Answer a2 ON sa.StudentAnswer = a2.AnswerId
WHERE
    (CorrectAnswer = '1')
ORDER BY $orderfield ASC";

如何将其输出为数组中的 $row['...']?

最佳答案

您发布的查询看起来不错,除了您需要在 WHERE 子句中为 CorrectAnswer 指定表别名,因为它不明确(可能来自 回答 a回答 a2:

WHERE
    (a.CorrectAnswer = '1')
     ^^ specify alias

工作演示:http://sqlize.com/Wk0LXJQ7De

此外,由于您的列名为 StudentAnswerContent,其中包含学生的回答文本,因此您需要在 PHP 中更改它:

<td>{$row['StudentAnswerContent']} </td>
                        ^^^^^^^ add "Content"

供将来引用:如果您发布准确的表架构,它会有所帮助。您缺少 Question (QuestionId, QuestionNumber) 表,Answer 中的列称为 CorrectAnswer 而不是 AnswerCorrect。也不知道 StudentIdSessionId 应该来自哪里。此外,如果您需要查询方面的帮助,在您正在处理的帖子中提出查询会有所帮助。

关于php - 将每个 'StudentAnswer' 字段显示为其关联的每个 'AnswerContent' 字段 (php),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7840833/

相关文章:

php - 将 1 个 <br/> 替换为 2 个正则表达式

php - 如何在 apache 和 php_cli 之间共享 APC 缓存?

mysql - 在 mysql 中模拟嵌套查询的完整外连接

mysql - 经典 ASP/MySQL - 参数问题

html - 在 IE7 中扩展出现在画廊下方的菜单

javascript - jQuery - 获取元素的单个计数

php - 使用 PHP 解析 Json 响应,数组键中有空格

php - 如何使用 Composer 安装 jQuery?

php - 使用 CodeIgniter 动态地用 mysql 数据填充 <select>

Javascript:警报显示多次