I have this code that will perform an insert to the question, choice and multichoice table. I managed to accomplish in inserting question and choice table, what I failed to do is the multichoice table.
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());
}
}
This is the sample output in the db.
question table
id q_name
1 1. What foo?
2 2. 2 + 2 is?
choice table
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
question in choice table is the id from question table
What I want to achieve in multichoice table.
multichoice table
id question mc_answers
1 1 1,2,3,4
2 2 5,6,7,8
question in multichoice table is the id from question table
I’m confused on how to do this, I would like to consult it to you guys. What should I do to achieve this?