Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:23+00:00 2026-05-23T16:20:23+00:00

Ok, so I am building an application, that allows the users to post a

  • 0

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");
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:20:24+00:00Added an answer on May 23, 2026 at 4:20 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question here! I'm building a simple application that allows users to create and
I have an application that allows users to save events. I would like to
Building an iPhone OS application that will allow users to anonymously post information to
I am building a web application that will need to allow users to save
I have an application that allows users to upload images. The test case I
I'm building an application that among other things allows users to upload documents. I
I am building an application that allows users to make changes to objects, but
I have a web application that allows users to upload certain documents relevant to
I am building an application based on MySQL that allows users to search for
G'day guys, I'm building an application that allows users to rapidly input numbers into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.