Ok, so I am building an application, that allows the users to post a question, and add 4 possible answers to that question. I have save the question to the database, get the inserted id and assign that to my answers so I can pull the corresponding answers back for the correct questions, however my foreach loop only fires once, as currently I am only adding 1 question, which means that only 1 answers gets added to the database. How can a rewrite my code so that the question gets saved and then a loop over the answers the correct number of times to add the 4 answers for the question?, My current code is below,
$count = count($questions);
for($i = 0; $i < $count; $i ++) {
if($this->questions_model->insert($questions[$i]))
{
$answers[$i]['questions_question_id'] = $this->db->insert_id();
if(!$this->answers_model->insert($answers[$i])) {
$errors = array("Something has gone wrong please try and submit again");
}
}
else
{
$errors = array("Something has gone wrong please try and submit again");
}
}