DB Table Structure:
Session Table (aka Exam Table)
SessionId(auto) SessionName
137 XULWQ
Question Table:
SessionId QuestionId OptionId
137 1 5
137 2 2
Option_Table Table:
OptionId OptionType
1 A-C
2 A-D
3 A-E
4 A-F
5 A-G
6 A-H
7 A-I
8 A-J
9 A-K
10 A-L
11 A-M
12 A-N
13 A-O
14 A-P
15 A-Q
16 A-R
17 A-S
18 A-T
19 A-U
20 A-V
21 A-W
22 A-X
23 A-Y
24 A-Z
25 True or False
26 Yes or No
Answer Table:
AnswerId(auto) SessionId QuestionId Answer
200 137 1 B
201 137 1 D
202 137 2 F
203 137 2 A
204 137 2 C
I want to create a page where I want it to display the incorrect answers per question.
I am thinking of doing it by retrieving the each question’s option type, display the all the letter answers belonging to the option type, and then remove the correct answers from the letter answers so that it is left with incorrect answers only.
options array:
$option = array();
$option[1]= array(A,B,C);
$option[2]= array(A,B,C,D);
$option[3]= array(A,B,C,D,E);
...
$option[23]= array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y);
$option[24]= array(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
$option[25]= array(True,False);
$option[26]= array(Yes,No);
My question is that I need help after this section. How do I start retrieving the wrong answers after this array using mysqli/php and the database I currently have?
UPDATE:
Below shows the sql which displays the correct answers for each question:
SELECT
q.QuestionContent,
o.OptionType,
q.NoofAnswers,
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS Answer,
r.ReplyType,
q.QuestionMarks
FROM Question q
LEFT JOIN Answer an
ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r
ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o
ON q.OptionId = o.OptionId
group by q.QuestionContent
This returns the result:
| QUESTIONCONTENT | OPTIONTYPE | NOOFANSWERS | ANSWER | REPLYTYPE | QUESTIONMARKS |
----------------------------------------------------------------------------------------
| Name these 2 flowers | A-F | 2 | C | Multiple | 5 |
| What is 2+2? | A-D | 1 | ABD | Single | 5 |
Your problem is that, the way your
Option_Tableis designed, decoding theOptionTypecolumn to find out all the possible answers requires some nontrivial external knowledge.(Indeed, you haven’t provided enough information in your question for me to be sure exactly how to do that; I can make a guess for those
OptionTypeswhich you’ve shown me, but I can’t be sure if there are, or could be, others.)It would be better to replace (or at least augment) that table with a simpler table which, for each
OptionId, simply lists all the possible options, like this:Then you can find all the right and wrong answers for each question with a query like this:
Here’s a demo of it on SQLize (slightly altered to skip columns not included in your example tables).
Edit: An alternative solution would be to construct the list of wrong answers in PHP. For example, if
$rowis an object (as returned e.g. bymysqli_fetch_object()) containing a single row from your original query, you could compute the wrong answers like this:Here’s a demo on ideone.com, based on your sample query output.