简体   繁体   English

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

[英]I want to database fields in an array to link with each other and then display the information

Please Read this carefully so you understand the question. 请仔细阅读此内容,以便您理解问题。 This question is for an assignment in University. 这个问题是给大学分配的。

There are two tables, one is an Answer table and the other is a StudentAnswer table. 有两个表,一个是Answer表,另一个是StudentAnswer表。 There are 6 fields I am interested in, 4 in Answer table and 2 from StudentAnswer table. 我感兴趣的有6个字段,“答案”表中有4个字段,而“学生答案”表中有2个字段。 Below are tables and their fields and data. 以下是表及其字段和数据。

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 field contain AnswerId's, these are student answers depending on which answer they selected for which question) (StudentAnswer字段包含AnswerId,这些是学生答案,具体取决于他们为哪个问题选择的答案)

Now these fields and other fields are stored in an array which are displayed in a table. 现在,这些字段和其他字段存储在表中显示的数组中。 PHP code for this is below: 如下的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>

I created a query which only displays only the correct answer (This is AnswerId where AnswerCorrect = 1) and it shows StudentAnswer so we can see what students have put down for their awnser to a particular question. 我创建了一个查询,该查询仅显示正确的答案(这是AnswerId,其中AnswerCorrect = 1),它显示了StudentAnswer,因此我们可以查看学生针对其特定问题回答了哪些问题。

I want the query to output one row per question with the text of the student's answer (but does have to be the correct answer) 我希望查询将每个问题的一行输出与学生答案的文本相同(但必须是正确的答案)

Below is query: 下面是查询:

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

The problem is that StudentAnswer is shown in int form and I want to show it in word form and the only way to do that is link to AnswerContent field. 问题是StudentAnswer以int形式显示,我想以单词形式显示,唯一的方法是链接到AnswerContent字段。 The problem is that if I just use $row['AnswerContent'], it displays the word answer for AnswerId which is fine but it doesn't do it for StudentAnswer. 问题是,如果我只使用$ row ['AnswerContent'],它将显示AnswerId单词的答案,这很好,但对于StudentAnswer则不行。 So my question is how can I do it so that I can display StudentAnswer as word form by linking it with AnswerContent? 所以我的问题是如何做到这一点,以便可以通过将它与AnswerContent链接起来以单词形式显示StudentAnswer?

I tried $row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent']; 我尝试了$row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent']; but this backfired completley. 但这事与愿违。 Can you figure this out for me and show me example on how to achieve this. 您能为我解决这个问题,并向我展示如何实现此目标的示例。

I like to say that the problem is NOT the query. 我想说的问题不是查询。 The query works fine. 该查询工作正常。 I want you to look at the $row['...'] section and tell me how can I link StudentAnswer and AnswerContent together so that it recognises what the correct AnswerContent is for each StudentAnswer. 我希望您查看$ row ['...']部分,并告诉我如何将StudentAnswer和AnswerContent链接在一起,以便它识别出每个StudentAnswer正确的AnswerContent是什么。

Below shows what it outputs at moment from the query and array: 下面显示了查询和数组现在输出的内容:

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

Below is what I really want to output: 以下是我真正想要输出的内容:

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

Row 1 shows what student u0867587 has put for his answer for question 1. The correct answer is leeds and he put for his answer leeds. 第1行显示了学生u0867587对问题1的回答。正确的答案是利兹,而他对答案的利兹做了回答。 Row 2 shows that student u1231231 answered the same question, obviously the correct answer is still leeds but he selected birmingham. 第2行显示学生u1231231回答了相同的问题,显然正确的答案仍然是利兹,但他选择了伯明翰。

Thank You and any help will be much appreciated 谢谢您,任何帮助将不胜感激

Full code with solution of the query and the form submitted to output the result: 包含查询解决方案和提交以输出结果的表单的完整代码:

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

SQL: 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: 行:

$row['StudentAnswerContent']

Explanation: 说明:

1st join - u get student answer 第一次加入-你得到学生的答案

2nd join - u get correct answer content 第二次加入-您获得正确的答案内容

3rd join - u get student answer content 第3次加入-您获得学生的答案内容

Your query isn't quite right. 您的查询不太正确。 You need another join to the Answer table using StudentAnswer to get the text. 您需要使用StudentAnswer再次连接到Answer表以获取文本。 Something like this should do it.. 这样的事情应该做。

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

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

相关问题 我想在填写每个文本字段或使用Ajax或jQuery选择类别后显示成功图像 - i want to display a success image after filling up each text fields or selecting the categories using ajax or jQuery 如何以表格格式显示数组,该表格格式在php中彼此优先 - How to display array in table format that has fields before each other in php 我试图获取结果页面以根据表单字段中的内容显示数据库信息 - I am trying to get the results page to display the database information based on what is in the form fields 数据库查询retuyrns字段以一种顺序显示,我想以另一种显示它们 - database query retuyrns fields in one order, I want to display them in another 我怎样才能创建一个数据库每年和 select 我想要一个 select 的年份并显示那一年的记录? - How can I create a database each year and select the year I want for a select and display the records for that year? 显示数组不是我想要的方式 - Display array not the way I want 我想获取该信息形式的数组php? - i want to fetch this information form that array php? 我想在每个循环之间显示一个项目 - i want to display an item in between a for each loop 我应该如何显示动态数据库信息? - How should I display dynamic database information? 从数据库中获取数组,然后使用每个数组值获取信息 - Grabbing array from database, then getting information using each array value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM