Udders
Udders

Reputation: 6976

help with a for loop and inserting data to a database

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");
                }
            }

Upvotes: 1

Views: 467

Answers (3)

Patrick
Patrick

Reputation: 3450

Don't use the normal for loop, too much work for you. Use foreach loops. Nest ones at that.

Try this:

if ( $this->questions_model->insert($question) )
{
  $question_id = $this->db->insert_id();

  foreach ($answers AS $answer)
  {
    $answer['questions_question_id'] = $question_id;

    if (!$this->answers_model->insert($answer)) {
      $errors = array("Something has gone wrong please try and submit again");
    }
  }
}
else
{
  $errors = array("Something has gone wrong please try and submit again");
}

The problem I see here is that if your questions and answers are coming from one big form then how do we know which answers go to which questions? Your best bet here is to only submit one question with multiple answers in the same form. Then the above code will work.

Otherwise you need to embed a way to attach the answers to each question ahead of time. Maybe even embed the array of answers in each question. In which case you can do this...

foreach ($questions AS $question)
{
  if ( $this->questions_model->insert($question) )
  {
    $question_id = $this->db->insert_id();

    foreach ($question->answers AS $answer)
    {
      $answer['questions_question_id'] = $question_id;

      if (!$this->answers_model->insert($answer)) {
        $errors = array("Something has gone wrong please try and submit again");
      }
    }
  }
  else
  {
    $errors = array("Something has gone wrong please try and submit again");
  }
}

Nested loops and function calls are your friend. If each loop gets much more complex I would recommend encapsulating parts of it into functions that you can call repetitively.

Upvotes: 0

drfranks3
drfranks3

Reputation: 665

You could run a secondary for loop to run through all the answers, if I understand your question correctly.

$count = count($questions);
for($i = 0; $i < $count; $i++) {
    if($this->questions_model->insert($questions[$i])) {
        for($e = 0; $e < count($answers); $e++) {
            if(!$answers[$e]) break; // Stop trying to add answers if this answer is blank/invalid.
            $answers[$e]['questions_question_id'] = $this->db->insert_id();
            if(!$this->answers_model->insert($answers[$e])) {
                $errors = array("Something has gone wrong please try and submit again");
            }
        }
    } else {
        $errors = array("Something has gone wrong please try and submit again");
    }
}

Upvotes: 0

John
John

Reputation: 590

This might work for you if I understood your question. If you always have 4 answers it should be fine, otherwise you'll have to deal with the gaps this creates, but should give you an idea how to handle it.

        $count = count($questions);
        $answerMax = 4;
        for($i = 0; $i < $count; $i ++) 
        {
            if($this->questions_model->insert($questions[$i]))
            {
                for($j = 0; $j < $answerMax; $j++)
                {
                    $curAnswer = $i * $answerMax + $j;
                    $answers[$curAnswer]['questions_question_id'] = $this->db->insert_id();
                    if(!$this->answers_model->insert($answers[$curAnswer])) 
                    {
                        $errors = array("Something has gone wrong please try and submit again");
                    }
                }
            }
            else
            {
                $errors = array("Something has gone wrong please try and submit again");
            }
        }

Upvotes: 1

Related Questions