2015-10-05 70 views
0
所有數組值

因此,這裏是我對他/她想要多少問題(可多選),以獲得來自用戶的輸入代碼:獲取環路

 Multiple choice: <input type = "text" name="MC"><br> 

     <input type = "submit" name = "confirm" value = "Confirm"> 

之後,這是代碼有多少的問題,系統將會生成:

<?php 

if(isset($_POST['confirm'])){ 

$MC = $_POST['MC']; 
echo "<form method = 'POST' name = 'items' action ='createquestions.php'>"; 
$items = 1; 

    for ($x = 1; $x <= $MC; $x++) { 

     echo "Question Number $items:"; echo "<input type = 'text' name = 'questions[]' style='width: 500px'><br><br>"; 
     echo "A. "; echo "<input type = 'text' name = 'ans1[]'>"; 
     echo "B. "; echo "<input type = 'text' name = 'ans2[]'><br>"; 
     echo "C. "; echo "<input type = 'text' name = 'ans3[]'>"; 
     echo "D. "; echo "<input type = 'text' name = 'ans4[]'><br>"; 
     echo "Correct Answer: "; echo "<input type = 'text' name ='cans[]'><br><br>"; 
     $items++; 

    } 
     echo "<input type ='submit' name = 'save' value = 'Save'>"; 
     echo "</form>"; 
} 
?> 
<?php 

的問題是,它只會保存用戶的最後輸入。 例如,我已經輸入在選擇題:--textbox這裏 - 此代碼將產生2個問題,8種選擇,2罐=正確的答案,但它只會保存第二個問題,答案和正確的答案。系統將不會得到第一個問題,答案和正確答案的記錄。

下面是代碼,我會插入它在數據庫上:

<?php 

    if(isset($_POST['save'])){ 

     $user_id = $_SESSION['id']; 
     $questions = $_POST['questions']; 
     $ans1 = $_POST['ans1']; 
     $ans2 = $_POST['ans2']; 
     $ans3 = $_POST['ans3']; 
     $ans4 = $_POST['ans4']; 
     $cans = $_POST['cans']; 


     foreach($questions as $q){ 
      echo "<input type = 'hidden' value = '$q'>"; 
     } 

     require_once('xcon.php'); 

     $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
       VALUES ('NULL','$user_id','$q','$ans1','$ans2','$ans3','$ans4','$cans')"; 
    $result = mysql_query($query); 

    if($result){ 
     echo 'Insert Success!'; 
    } 
    else{ 
     echo 'Error'; 
    } 

} 

?> 
+0

對我來說,它提供了兩個問題的答案,如果我給兩個。你能再詳述一下你的問題嗎? –

回答

0

當你保存時,你應該再次運行一個循環。試試這可能嗎?

<?php 

    if(isset($_POST['save'])){ 
     $user_id = $_SESSION['id']; 
     require_once('xcon.php'); 
     foreach ($_POST['questions'] as $key => $question){ 
      $ans1 = $_POST['ans1'][$key]; 
      $ans2 = $_POST['ans2'][$key]; 
      $ans3 = $_POST['ans3'][$key]; 
      $ans4 = $_POST['ans4'][$key]; 
      $cans = $_POST['cans'][$key]; 


      echo "<input type = 'hidden' value = '$question'>"; 

      $query = "INSERT INTO mcq (mc_id, user_id, questions, ans1, ans2, ans3, ans4, cans) 
        VALUES ('NULL','$user_id','$question','$ans1','$ans2','$ans3','$ans4','$cans')"; 
      $result = mysql_query($query); 

      if($result){ 
       echo 'Insert Success!<br>'; 
      }else{ 
       echo 'Error<br>'; 
      } 
     }  
    } 

?> 
+0

它工作!非常感謝 ! :) –

0

根據this post你應該使用:

echo "Question Number $items:"; echo "<input type = 'text' name = 'questions' style='width: 500px'><br><br>"; 
echo "A. "; echo "<input type = 'text' name = 'ans[]'>"; 
echo "B. "; echo "<input type = 'text' name = 'ans[]'><br>"; 
echo "C. "; echo "<input type = 'text' name = 'ans[]'>"; 
echo "D. "; echo "<input type = 'text' name = 'ans[]'><br>"; 
echo "Correct Answer: "; echo "<input type = 'text' name ='cans'><br><br>"; 

而且這樣的:

$ans1 = $_POST['ans'][0]; 
$ans2 = $_POST['ans'][1]; 
$ans3 = $_POST['ans'][2]; 
$ans4 = $_POST['ans'][3]; 

說明

你只需要反覆張貼ans[],而不是

ans1[], ans2[], ans3[]...爲了得到這樣

$_POST['ans'][0], $_POST['ans'][1]...

或陣列可以使用

ans1, ans2, ans3

(不含括號[] )閱讀爲

$_POST['ans1'], $_POST['ans2'], $_POST['ans3']...

+0

雖然他需要很多ans1,ans2,ans3和ans4。每個問題一個。命名他們ans []將創建四個每個問題,因爲他目前的方式將有1個ans1,ans2,ans3和ans4每個問題。 – Christian

0

您正在以一種奇怪的方式使用元素命名,您使用的是數組,但仍然使用數字。嘗試產生這樣的:

for ($x = 0; $x <= $MC; $x++) { 
    echo "<input type = 'text' name = 'questions[$i]'>"; 
    echo "A. <input type = 'text' name = 'ans[$i][A]'>"; 
    echo "B. <input type = 'text' name = 'ans[$i][B]'><br>"; 
    echo "C. <input type = 'text' name = 'ans[$i][C]'>"; 
    echo "D. <input type = 'text' name = 'ans[$i][D]'><br>"; 
    echo "Correct Answer: <input type = 'text' name ='cans[$i]'><br><br>"; 
} 

然後你會得到下面的結果在您的$_POST

[ 
    "questions" => [ 
     0 => "question1", 
     ... 
    ] 
    "ans" => [ 
     0 => [ 
      "A" => "answer A", 
      "B" => "answer B", 
      "C" => "answer C", 
      "D" => "answer D", 
     ] 
     ... 
    ] 
    "cans" => [ 
     0 => "A", 
     .... 
    ] 
] 

這是很容易在foreach處理:

foreach ($_POST['questions'] as $key => $question) { 
    // $question == 'question1'; 
    $answers = $_POST['ans'][$key]; // array of answers 
    $solution = $_POST['cans'][$key]; 
}