2011-07-13 95 views
1

好吧,所以我正在構建一個應用程序,它允許用戶發佈問題,併爲該問題添加4個可能的答案。我已經將問題保存到數據庫中,獲取插入的ID並將其分配給我的答案,以便我可以將相應的答案返回給正確的問題,但是我的foreach循環只觸發一次,因爲目前我只添加1個問題,意味着只有1個答案被添加到數據庫中。如何能重寫我的代碼,以便問題得到保存,然後一個循環在回答正確的次數增加了4個答案的問題?我當前的代碼如下,help for循環並將數據插入數據庫

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

這是一個相當奇怪的問題,因爲我無法在代碼中找到foreach循環。 for循環看起來很好,但在計算答案時可能會出錯,您傳遞給它的數組的print_r可能會給你一些有用的見解。 – bigblind

+1

使用foreach! http://php.net/manual/en/control-structures.foreach.php – dwerner

+0

您可以閱讀編輯器幫助,並瞭解如何格式化您的代碼:http://stackoverflow.com/editing-help。謝謝。 – Kev

回答

0

不要使用正常的循環,對你來說工作太多。使用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"); 
    } 
} 

嵌套循環和函數調用是你的朋友。如果每個循環變得更加複雜,我會建議將它的部分封裝到可以重複調用的函數中。

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

如果我正確理解您的問題,您可以運行第二個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"); 
    } 
}