1

I use this code below to parse a question and its choices.

$entries = preg_split('/(?=[a-z\d]+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY);
$arrAnswers = array();
$arrQuestions = array();
$id = 0; //Assuming your table is empty

foreach($entries as $entry) { //Loop through the grabbed records
  if(is_numeric(substr($entry, 0, 1)) === true) { //Is it a question?
     $id++;     
     $arrAnswers[$id] = array();
     $arrQuestions[$id] = '(\''. $entry .'\')'; 
  } else { //Ok, it's a possible answer to a question
     $arrAnswers[$id][] = '(\''. $entry .'\', '. $id .', 0)';
  }
}
      echo "<pre>";
        print_r($arrQuestions);
      echo "<pre>";

      echo "<br>";

      echo "<pre>";
        print_r($arrAnswers);
      echo "<pre>";

Which returns these array result:

$arrQuestions output

Array
    (
        [1] => ('1. What is foo?')
        [2] => ('2. What is foo?')
    )

$arrAnswers output
    Array
    (
        [1] => Array
            (
                [0] => ('a. foo1', 1, 0)
                [1] => ('b. foo2', 1, 0)
                [2] => ('c. foo3', 1, 0)
                [3] => ('d. foo4', 1, 0)
            )

        [2] => Array
            (
                [0] => ('a. foo1', 2, 0)
                [1] => ('b. foo2', 2, 0)
                [2] => ('c. foo3', 2, 0)
                [3] => ('d. foo4', 2, 0)
            )

    )

My code for the insertion of records:

try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$strDbQuery = "INSERT INTO `question` (`q_name`) VALUES ". implode(", ", $arrQuestions);
$strDbQuery_2 = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ". implode(", ", $arrAnswers);
// use exec() because no results are returned
$conn->exec($strDbQuery);
$conn->exec($strDbQuery_2);
echo "New questions uploaded successfully";
}
catch(PDOException $e)
{
echo $strDbQuery . "<br>" . $e->getMessage();
echo $strDbQuery_2 . "<br>" . $e->getMessage();
}

$conn = null;

I printed the the message of the transaction and on the insertion of the answers, the value being inserted is an array. I used the function implode() but it is still treated as an array.

INSERT INTO `question` (`q_name`) VALUES ('1. What is foo1?'), ('2. What is foo2?')  
INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES Array, Array

What should I do guys?

4
  • edit with the output of print_r($arrAnswers); Commented Feb 22, 2015 at 2:06
  • @AdrianCidAlmaguer I updated my question, terribly sorry if my output was not clear, thanks for pointing it out! Commented Feb 22, 2015 at 2:17
  • implode() doesn't work for multidimensional arrays. You can try this solution: stackoverflow.com/questions/16710800/… Commented Feb 22, 2015 at 2:31
  • @rodrigovr I appreciate your help, this is informative. Commented Feb 22, 2015 at 2:34

1 Answer 1

2

Edit: More generic

$strDbQuery_2 = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ";

for($i = 1; $i <= count($arrAnswers); $i++) {
    $strDbQuery_2 .= implode(", ", $arrAnswers[$i]) . ', ';
}

$strDbQuery_2 = substr($strDbQuery_2, 0, -2) . ';';
Sign up to request clarification or add additional context in comments.

1 Comment

This really helps and thorough!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.