【发布时间】:2015-04-06 06:52:23
【问题描述】:
我有这段代码可以插入问题、选择和多项选择表。我设法完成了插入问题和选择表,我没有做的是多选表。
require("dbOption/Db.class.php");
$question = new Db();
$choice = new Db();
$multichoice = new Db();
$entries = array(
0 => '1. What foo?',
1 => 'a. foo1',
2 => 'b. foo2',
3 => 'c. foo3',
4 => 'd. foo4',
5 => '2. 2 + 2 is?',
6 => 'a. test1',
7 => 'b. test2',
8 => 'c. test3',
9 => 'd. test4',
);
$answerIDs = "";
$questionID = "";
$multipleChoice = array();
foreach ($entries as $entry) {
if(is_numeric(substr($entry, 0, 1)) === true) {
echo "<pre>";
var_dump($entry);
echo "<pre>";
$question->query("INSERT INTO question(q_name) VALUES(:question)",array("question"=>$entry));
$questionID = $question->lastInsertId();
} else {
echo "<pre>";
var_dump($entry);
echo "<pre>";
$answer->query("INSERT INTO choice(choices,question) VALUES(:choices, :question)",array("choices"=>$entry, "question"=>$questionID));
if ($answerIDs === "")
$answerIDs = $choice->lastInsertId();
else
// store last inserted ids in choice table and separate it with ","
$answerIDs .= ("," . $choice->lastInsertId());
}
}
这是数据库中的示例输出。
问题表
id q_name
1 1. What foo?
2 2. 2 + 2 is?
选择表
id choices question correct
1 a. foo1 1 0
2 b. foo2 1 0
3 c. foo3 1 0
4 d. foo4 1 0
5 a. test1 2 0
6 b. test2 2 0
7 c. test3 2 0
8 d. test4 2 0
选择表中的问题是问题表
中的id
我想在多选表中实现什么。
多选表
id question mc_answers
1 1 1,2,3,4
2 2 5,6,7,8
question multichoice table 是 question table
中的id
我对如何做到这一点感到困惑,我想向你们咨询。我应该怎么做才能做到这一点?
【问题讨论】:
-
听起来设计很糟糕。您已经可以通过查找问题 id 找到问题的所有答案,因此现在您希望以错误(逗号分隔)的方式存储冗余信息。
-
没有区别,我只是编辑了我的问题并添加了一些细节......
-
其实我是在模仿 Moodle db 中的某个设计,多选题。我为糟糕的设计道歉......