douxuan1284 2011-07-13 22:42
浏览 56
已采纳

帮助for循环并将数据插入数据库

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");
                }
            }
  • 写回答

3条回答 默认 最新

  • dtnwm4807 2011-07-13 22:54
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景