我希望数组中的数据库字段相互链接,然后显示信息

时间:2011-10-19 18:00:03

标签: php mysql database

请仔细阅读以便您理解这个问题。这个问题适用于大学的作业。

有两个表,一个是Answer表,另一个是StudentAnswer表。我感兴趣的有6个领域,答案表有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>";
        }
      ?>
    </table>

我创建了一个只显示正确答案的查询(这是AnswerId,其中AnswerCorrect = 1),它显示了StudentAnswer,因此我们可以看到学生为特定问题找到了什么。

我希望查询每个问题输出一行,并附上学生答案的文本(但必须是正确答案)

以下是查询:

SELECT * FROM Question q
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
    JOIN Answer a ON sa.QuestionId = a.QuestionId  
WHERE
    (CorrectAnswer = '1')
ORDER BY $orderfield ASC";

问题是StudentAnswer以int形式显示,我想以word形式显示它,唯一的方法是链接到AnswerContent字段。问题是,如果我只使用$ row ['AnswerContent'],它会显示AnswerId的单词answer,这很好但是它不适用于StudentAnswer。所以我的问题是如何才能将StudentAnswer显示为单词形式,将其与AnswerContent链接?

我试过了$row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent'];,但这种情况适得其反。你能为我解决这个问题,并向我展示如何实现这个目标。

我想说问题不是查询。查询工作正常。我希望你看一下$ row ['...']部分并告诉我如何将StudentAnswer和AnswerContent链接在一起,以便它能识别每个StudentAnswer的正确AnswerContent。

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

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

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

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

第1行显示了学生u0867587为问题1的答案所提出的答案。正确的答案是利兹,他提出了他的答案。第2行显示学生u1231231回答了同样的问题,显然正确的答案仍然是利兹,但他选择了伯明翰。

谢谢你,非常感谢任何帮助

完整代码,包含查询解决方案和提交的表单以输出结果:

<form action="exam_QA.php" method="post" name="sessionform">        <!-- This will post the form to its own page"-->
      <p>Session ID: <input type="text" name="sessionid" value="<?php echo $sessionid; ?>" /></p>      <!-- Enter Session Id here-->
      <p>Question Number: <input type="text" name="questionno" value="<?php echo $questionno; ?>" /></p>      <!-- Enter Question Number here-->
      <p>Student Username: <input type="text" name="studentid" value="<?php echo $studentid; ?>" /></p>      <!-- Enter User Id here-->
      <p>Order Results By:
        <select name="orderfield">
          <option value="ordersessionid"<?php if ($orderfield == 'q.SessionId') echo ' selected="selected"' ?>>Session ID</option>
          <option value="orderquestionno"<?php if ($orderfield == 'q.QuestionNo') echo ' selected="selected"' ?>>Question Number</option>
          <option value="orderstudentid"<?php if ($orderfield == 'sa.StudentId') echo ' selected="selected"' ?>>Student Username</option>
          <option value="orderwhole"<?php if ($orderfield == 'q.SessionId AND q.QuestionNo') echo ' selected="selected"' ?>>Session ID and Question Number</option>
        </select>
      </p>
      <p><input type="submit" value="Submit" name="submit" /></p>
    </form>

    <?php
      if (isset($_POST['submit'])) {

        $query = "
         SELECT *, a2.AnswerContent as StudentAnswerContent
         FROM Question q
        INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
        LEFT JOIN Answer a ON (sa.QuestionId = a.QuestionId AND a2.CorrectAnswer = 1) 
        LEFT JOIN Answer a2 ON (sa.QuestionId = a2.QuestionId AND a2.AnswerId = sa.StudentAnswer) 
          WHERE
            ('".mysql_real_escape_string($sessionid)."' = '' OR q.SessionId = '".mysql_real_escape_string($sessionid)."')
          AND
            ('".mysql_real_escape_string($questionno)."' = '' OR q.QuestionNo = '".mysql_real_escape_string($questionno)."')
          AND
            ('".mysql_real_escape_string($studentid)."' = '' OR sa.StudentId = '".mysql_real_escape_string($studentid)."')
          ORDER BY $orderfield ASC";

2 个答案:

答案 0 :(得分:0)

<强> SQL:

SELECT 
    q.* ,
    sa.*,
    a.*,
    a2.AnswerContent as StudentAnswerContent
FROM 
    Question q
INNER JOIN 
    StudentAnswer sa ON q.QuestionId = sa.QuestionId
LEFT JOIN 
    Answer a ON (sa.QuestionId = a.QuestionId AND a2.CorrectAnswer = 1) 
LEFT JOIN
    Answer a2 ON (sa.QuestionId = a2.QuestionId AND a2.AnswerId = sa.StudentAnswer)
ORDER BY $orderfield ASC";

<强>行:

$row['StudentAnswerContent']

<强>解释

第一次加入 - 你得到学生回答

第二次加入 - 你得到正确答案内容

第3次加入 - 你得到学生答案内容

答案 1 :(得分:0)

您的查询不太正确。您需要使用StudentAnswer另一个连接到答案表来获取文本。像这样的东西应该这样做..

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