There are two tables, one is an Answer table and the other is a StudentAnswer table. There are 6 fields I am interested in, 4 in Answer table and 2 from StudentAnswer table. 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)
Now these fields and other fields are stored in an array which are displayed in a table. PHP code for this is below:
<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>";
}
?>
Below shows what it outputs at moment from the query and array:
Session ID Question Number Answerid AnswerContent StudentAnswer Student ID
ABB 1 1 Leeds 1 u0867587
ABB 1 1 Leeds 3 u1231231
Row 1 shows what student u0867587 has put for his answer for question 1. The correct answer is leeds and as he selected answerid ‘1’ which is Leeds, his student answer is Leeds but obviously as StudentAnswer is an int field it displays it as ‘1’. Row 2 shows that student u1231231 answered the same question, obviously the correct answer is still leeds but he selected birmingham which is answerid ‘3’ so his answer is Birmingham but as StudentAnswer is int field it dislays it as ‘3’ The AnswerContent is linked to the AnswerId field so it whows word answers for AnswerId
Below is what I really want to output:
Session ID Question Number AnswerId AnswerContent StudentAnswer Student ID
ABB 1 1 Leeds Leeds u0867587
ABB 1 1 Leeds Birmingham u1231231
It outputs the same as the one above but instead of displaying it as an int from StudentAnswer field, it displays it as the word answer using AnswerContent. So AnswerContent is linked to Answerid automatically but I also want to link it with StudentAnswer.
StudentAnswer field is the same as AnswerId as it retrieves the AnswerId depending on what the student has chosen for his answer.
I did try $row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent']; but it hasn’t worked. How can I display Answercontent for each StudentAnswer?
Below is query:
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";
How do I output it as a $row[‘…’] in the array?
Your query as posted looks fine, except you will need to specify the table alias for
CorrectAnswerin yourWHEREclause because it is ambiguous (could come from eitherAnswer aorAnswer a2:Working Demo: http://sqlize.com/Wk0LXJQ7De
Also, since your column is called
StudentAnswerContentthat contains the student’s answer text, you will need to change that in yourPHP:For future reference: It helps if you post an accurate table schema. You are missing
Question (QuestionId, QuestionNumber)table, and the column inAnsweris calledCorrectAnswernotAnswerCorrect. Also have no idea whereStudentIdandSessionIdare supposed to come from. Also, if you want help with a query, it kinda helps to have the query in the your post that you’re working with.