I use this code where it groups the question together with the choices.
<?php
$entries = preg_split('/(?=[a-z\d]+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY);
$questions = array();
$currentQuestion = null;
$id = 0;
foreach($entries as $entry) {
if(is_numeric(substr($entry, 0, 1)) === true) {
$currentQuestion = $entry;
$questions[$entry] = array();
$id++;
// echo "INSERT INTO question (id, q_name) VALUES ($id, $currentQuestion)"."<br>";
// mysqli_query($con, "INSERT INTO question (id, q_name) VALUES (NULL, '$currentQuestion')");
continue;
}
// mysqli_query($con, "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 0);");
// echo "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 'stuff')"."<br>";
$questions[$currentQuestion][] = $entry;
}
This is the result of the array.
Array
(
[1. What is love?] => Array
(
[0] => a. Haddaway
[1] => b. Haxxaway
[2] => c. Hassaway
[3] => d. Hannaway
)
[2. What is love? ] => Array
(
[0] => a. Haddaway
[1] => b. Haxxaway
[2] => c. Hassaway
[3] => d. Hannaway
)
[3. What is love 1.1? ] => Array
(
[0] => a. Haddaway
[1] => b. Haxxaway
[2] => c. Hassaway
[3] => d. Hannaway
)
[4. What is love? ] => Array
(
[0] => a. Haddaway
[1] => b. Haxxaway
[2] => c. Hassaway
[3] => d. Hannaway
)
)
And this is my database structure: question column in table answers is the primary key from questions table, that will determine which question the choice belongs...
questions
+-------+--------------------------+
| id | q_name |
+-------+--------------------------+
| 1 | 1.) What is foo? |
| 2 | 2.) What is foo? |
+-------+--------------------------+
answers
+-------+-------------+-----------------------+
| id | choices | question | correct |
+-------+-------------+-----------------------+
| 1 | a. foo1 | 1 | 0 |
| 2 | b. foo2 | 1 | 0 |
| 3 | c. foo3 | 1 | 1 |
| 4 | a. foo3 | 2 | 0 |
| 5 | b. foo2 | 2 | 1 |
| 6 | c. foo1 | 2 | 0 |
+-------+-------------+-----------------------+
I managed to insert the questions to db but I'm having trouble to insert the choices because I'm confused on what I should do to $questions in order to get the choices...
Any suggestion(s) would do!
answersandquestions(SHOW CREATE TABLE answers;)?