数据库表结构:
会话表(又名考试表)
SessionId(auto) SessionName
137 XULWQ
问题表:
SessionId QuestionId OptionId
137 1 5
137 2 2
Option_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
答案表:
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
我想创建一个页面,我希望它显示每个问题的错误答案.
我正在考虑通过检索每个问题的选项类型,显示属于选项类型的所有字母答案,然后从字母答案中删除正确答案,以便仅留下不正确的答案.
选项数组:
$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);
我的问题是,在本节之后我需要帮助.如何使用mysqli / php和我目前拥有的数据库开始检索此数组后的错误答案?
更新:
下面显示了sql,它显示了每个问题的正确答案:
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
这将返回结果:
| 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 |
最佳答案 您的问题在于,Option_Table的设计方式,解码OptionType列以找出所有可能的答案需要一些非常重要的外部知识.
(事实上,你没有在我的问题中提供足够的信息,以确定如何做到这一点;我可以猜测你向我展示的那些OptionTypes,但我不能确定是否有,或者可能是,其他人.)
用更简单的表替换(或至少扩充)该表会更好,对于每个OptionId,它只列出所有可能的选项,如下所示:
CREATE TABLE Options (
OptionId INTEGER NOT NULL,
OptionAnswer CHAR(1) NOT NULL, -- or whatever type Answer.Answer has
PRIMARY KEY (OptionId, OptionAnswer)
);
INSERT INTO Options VALUES
(1, 'A'), (1, 'B'), (1, 'C'),
(2, 'A'), (2, 'B'), (2, 'C'), (2, 'D'),
-- ...
(25, 'T'), (25, 'F'),
(26, 'Y'), (16, 'N');
然后,您可以使用以下查询找到每个问题的所有正确和错误答案:
SELECT
q.QuestionContent,
q.NoofAnswers,
GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR '') AS RightAnswers,
GROUP_CONCAT(DISTINCT
CASE
WHEN Answer IS NULL THEN OptionAnswer
ELSE NULL
END
ORDER BY OptionAnswer SEPARATOR '') AS WrongAnswers,
r.ReplyType,
q.QuestionMarks
FROM Question q
LEFT JOIN Reply r
ON q.ReplyId = r.ReplyId
LEFT JOIN Options o
ON q.OptionId = o.OptionId
LEFT JOIN Answer an
ON q.QuestionId = an.QuestionId AND o.OptionAnswer = an.Answer
GROUP BY q.SessionId, q.QuestionId
这是一个demo of it on SQLize(稍微修改为跳过未包含在示例表中的列).
编辑:另一种解决方案是在PHP中构造错误答案的列表.例如,如果$row是一个对象(如mysqli_fetch_object()
所返回的那样),其中包含原始查询中的一行,则可以计算错误的答案,如下所示:
// Do this (preferably) before looping over the rows:
$specialOptionTypes = array(
'Yes or No' => array( 'Y', 'N' ),
'True or False' => array( 'T', 'F' ),
);
// Do this for each row:
if ( array_key_exists( $row->OptionType, $specialOptionTypes ) ) {
$options = $specialOptionTypes[ $row->OptionType ];
} else if ( preg_match( '/^([A-Z])-([A-Z])$/', $row->OptionType, $match ) ) {
$options = range( $match[1], $match[2] );
} else {
// issue warning about unrecognized option type
$options = array();
}
$right = str_split( $row->Answer ); // or explode() on a delimiter, if any
$wrong = array_diff( $options, $right );
$row->WrongAnswers = implode( '', $wrong ); // if you actually want a string
Here’s a demo on ideone.com,基于您的示例查询输出.