我有这样的代码,它将对问题、选择和多选择表执行插入。我成功地完成了插入问题和选择表的工作,但我做不到的是多重选择表。
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());
}
}这是db中的示例输出。
试题表
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多选表中的问题是试题表中的id
我不知道该怎么做,我想和你们商量一下。我应该怎么做才能做到这一点?
发布于 2015-04-06 08:25:18
就像我说的,我认为你根本不应该储存那些清单。存储逗号分隔的值通常被认为是错误的设计,而且数据库中已经有了所需的所有信息,因此信息也是多余的。
如果你想这么做,我想有四种解决方案。
1)当你开始一个新的问题时插入答案。的缺点是你必须在循环之后再做同样的事情来保存最后一个问题的选择。代码的结构将如下所示(为了简洁起见,将其简化)。
$answerIDs = "";
foreach ($entries as $entry) {
if(is_numeric(substr($entry, 0, 1)) === true) {
if ($answerIDs !== "") {
// First insert multi select answers
// Reset the answers for the new question.
$answerIDs = "";
}
// Then insert question as you do now.
} else {
// Insert answers and store ID in $answerIDs as you do now.
}
}
if ($answerIDs !== "") {
// Store answers (again).
}2)将ID保存在一个大数组中,然后存储这些ID。可以将答案保存在嵌套数组中。主级别的关键是问题ID,每个问题都存储一个数组(或字符串,但我认为数组更容易)。插入问题和答案后,可以循环遍历数组并插入所有多个选项。
除非你有数以百万计的问题,否则一切都会好起来的。最终,$allAnswerIDs可能会变得内存太大,但我认为在这种情况下它不会很快成为一个问题。
$allAnswerIDs = array();
foreach ($entries as $entry) {
if(is_numeric(substr($entry, 0, 1)) === true) {
// Insert question as you do now.
// Store the question ID in the array.
$allAnswerIDs[$questionId] = array();
} else {
// Insert answer.
// Store ID in $answerIDs as an array (or as a string, if you like).
$allAnswerIDs[$questionID][] = $answerID;
}
}
foreach ($allAnswerIDs as $questionID => $questionAnswerIDs) {
// Build a string of the answers per question.
$answerIDs = implode(',', $questionAnswerIDs);
// Insert the multiple choice row using $questionID and $answerIDs.
}3)在循环之后使用大容量插入。您可以编写一个单一的、稍微复杂一些的SQL语句,同时插入所有这些语句,例如:
INSERT INTO multichoice(question, mc_answers)
SELECT
question,
GROUP_CONCAT(choices) as AnswerIDs
FROM
Choice您可以在脚本的末尾执行此语句一次,它将使数据库一次为多重选择表生成整个内容。
4)创建一个视图。正如您所看到的,方法3中的(相对简单的) select显示了您希望在多选表中拥有的所有信息,所以您可以考虑在数据库中将其改为视图,而不是将其插入表中。这样,你就不需要存储信息了。PHP代码更简单,数据库更干净,您可以像从表中一样从视图中进行查询。
https://stackoverflow.com/questions/29466734
复制相似问题