【问题标题】:help with a for loop and inserting data to a database帮助 for 循环和将数据插入数据库
【发布时间】:2011-07-13 22:42:15
【问题描述】:

好的,所以我正在构建一个应用程序,它允许用户发布问题,并为该问题添加 4 个可能的答案。我已将问题保存到数据库中,获取插入的 id 并将其分配给我的答案,以便我可以将相应的答案拉回正确的问题,但是我的 foreach 循环只触发一次,因为目前我只添加 1 个问题,其中意味着只有 1 个答案被添加到数据库中。如何重写我的代码以保存问题,然后循环正确次数的答案以添加问题的 4 个答案?,我当前的代码如下,

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

【问题讨论】:

  • 这是一个很奇怪的问题,考虑到我在您的代码中找不到 foreach 循环这一事实。 for 循环看起来不错,但是在计算答案时可能会出现问题,您传递给它的数组的 print_r 可能会给您一些有用的见解。
  • 您能否阅读编辑器帮助并了解如何格式化您的代码:stackoverflow.com/editing-help。谢谢。

标签: php arrays codeigniter for-loop


【解决方案1】:

如果我理解您的问题,这可能对您有用。如果你总是有 4 个答案应该没问题,否则你将不得不处理由此造成的差距,但应该让你知道如何处理它。

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

【讨论】:

    【解决方案2】:

    不要使用普通的 for 循环,对你来说工作量太大。使用 foreach 循环。在那里筑巢。

    试试这个:

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

    我在这里看到的问题是,如果您的问题和答案来自一个大表格,那么我们如何知道哪些答案对应哪些问题?最好的办法是在同一个表格中只提交一个带有多个答案的问题。那么上面的代码就可以工作了。

    否则,您需要嵌入一种方法来提前附加每个问题的答案。甚至可能在每个问题中嵌入一系列答案。在这种情况下,您可以这样做...

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

    嵌套循环和函数调用是您的朋友。如果每个循环变得更加复杂,我建议将其部分封装到可以重复调用的函数中。

    【讨论】:

      【解决方案3】:

      如果我正确理解您的问题,您可以运行第二个 for 循环来遍历所有答案。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-13
        • 1970-01-01
        • 2023-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多