2017-08-06 47 views
0

iam與問題頁面工作,每個用戶將向他展示2個或更多的問題每個問題有4個答案和4個(單選按鈕),每個問題必須從其他問題,這基本上意味着曾經單選按鈕組必須是像這樣,我的問題是像圖像波紋管(我不能發佈他們全部)單選按鈕組與別人分開使用php while循環dosn't工作

see my problem

我有使用此代碼(不工作)

$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 "; 
$result2 = mysqli_query($conn,$sql2); 

       echo "<form action='' method='POST'>"; 

        while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) { 

        echo "<div align='center'> 
        <table> 

         <thead> 
         <tr > 
         <th >Q & A</th> 
         <th>Choose</th> 
         </tr> 
         </thead> 
         <tbody> 
         <tr> 
         <td>".$row2['question']."</td> 
         <td></td> 
         </tr> 
         <tr> 
         <td>".$row2['ans1']."</td> 
         <td><input type='radio' name='chk' value='1' > 
         </tr> 
         <tr> 
         <td>".$row2['ans2']."</td> 
         <td><input type='radio' name='chk' value='2' > 
         </tr> 
         <tr> 
         <td>".$row2['ans3']."</td> 
         <td><input type='radio' name='chk' value='3' > 
         </tr> 
         <tr> 
         <td>".$row2['ans4']."</td> 
         <td><input type='radio' name='chk' value='4' > 
         </tr> 
         </tbody> 
        </table><br> <br> 
        </div> 
        "; 

        } 
        echo "<input type='submit' name='submit'> 
        </form>"; 
        if (isset($_POST['submit'])) { 

         $chk = $_POST['chk']; 

         Echo $chk; 

        } 

即使我贊同這樣的

print_r(array_values($chk)); 

,如果我每次都使用我不能得到名稱不同的名稱,我用這個代碼,同時循環中,其確定的作品,但如何讓其他表的價值?

$new = 0; 
<input type='radio' name='chk".$new."' value='some value' > 
$new++; 
+0

嗨,我可以幫助你,但無法理解其中的實際問題。 –

+0

你能發表這個查詢結果嗎?SELECT * FROM questions where'subject''web lang'LIMIT 5' –

+0

用戶必須爲每個單獨的問題選擇1個單選按鈕,當他點擊提交按鈕時,它應該給我所有他選擇的答案(但它dosnt做到這一點) – MrB

回答

1

試試這個:

$sql2 = "SELECT * FROM questions WHERE subject = 'web lang' LIMIT 5 "; 
$result2 = mysqli_query($conn,$sql2); 

       echo "<form action='' method='POST'>"; 
       $i=0; 
        while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) { 

        echo "<div align='center'> 
        <table> 

         <thead> 
         <tr > 
         <th >Q & A</th> 
         <th>Choose</th> 
         </tr> 
         </thead> 
         <tbody> 
         <tr> 
         <td>".$row2['question']."</td> 
         <td></td> 
         </tr> 
         <tr> 
         <td>".$row2['ans1']."</td> 
         <td><input type='radio' name='chk".$i."' value='1' > 
         </tr> 
         <tr> 
         <td>".$row2['ans2']."</td> 
         <td><input type='radio' name='chk".$i."' value='2' > 
         </tr> 
         <tr> 
         <td>".$row2['ans3']."</td> 
         <td><input type='radio' name='chk".$i."' value='3' > 
         </tr> 
         <tr> 
         <td>".$row2['ans4']."</td> 
         <td><input type='radio' name='chk".$i."' value='4' > 
         </tr> 
         </tbody> 
        </table><br> <br> 
        </div> 
        "; 
        $i++; 
        } 
        echo "<input type='submit' name='submit'> 
        </form>"; 
        if (isset($_POST['submit'])) { 

         $chk = $_POST['chk']; 

         Echo $chk; 

        } 
+1

在您的發佈請求中,您應該看到已發送的'chk'的數量 –

+0

我做了這些但仍然是一個問題,那就是(我怎麼能獲得發佈的名稱?我應該像這樣使用'$ chk = $ _POST ['chk'+ $ i];'和echo'$ chk + $ i;'' – MrB

+1

您應該能夠從$ _POST變量訪問它們: '的foreach($ _ POST爲$ PARAM_NAME => $ param_val){ 回聲 「帕拉姆:$ PARAM_NAME;價值:$ param_val
\ n」; }' –

1

假設的事實,你必須與每一個問題相關的questionID列,改變你的while循環通過以下方式,

// your code 
echo "<form action='' method='POST'>"; 
while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) { 
    ?> 
    <div align='center'> 
    <table> 
     <thead> 
      <tr > 
       <th >Q & A</th> 
       <th>Choose</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td><?php echo $row2['question']; ?></td> 
       <td></td> 
      </tr> 
      <tr> 
       <td><?php echo $row2['ans1']; ?></td> 
       <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='1' > 
      </tr> 
      <tr> 
       <td><?php echo $row2['ans2']; ?></td> 
       <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='2' > 
      </tr> 
      <tr> 
       <td><?php echo $row2['ans3']; ?></td> 
       <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='3' > 
      </tr> 
      <tr> 
       <td><?php echo $row2['ans4']; ?></td> 
       <td><input type='radio' name='chk[<?php echo $row2['quesrtionID']; ?>]' value='4' > 
      </tr> 
     </tbody> 
    </table> 
    <br> <br> 
    </div> 
    <?php 
} 
echo "<input type='submit' name='submit'> 
</form>"; 
// your code 

後來當用戶選擇無線電選項並點擊提交按鈕時,獲取問題ID和fo中的相應(給定)答案一路走來,

if (isset($_POST['submit'])) { 
    foreach($_POST['chk'] as $questionID => $answer){ 
     // $questionID is the question ID and $answer is 
     // the corresponding given answer of that quesrtion 
    } 
} 
+0

哇另一個完美的答案:),感謝人正是我所尋求的,但我可以插入這些答案到表?我應該像這樣使用 '$ AnsSQL =「INSERT INTO students(id,subject,question,answer) VALUES(null,'web lang','$ id','$ answer')」; if(mysqli_query($ conn,$ AnsSQL)){ echo「sent」; }' – MrB

+1

@MrB是的,你當然可以做到這一點。但是,您應該創建* id *列'AUTO INCREMENT'。這樣你就不必在查詢中明確地提到,只要你做'$ AnsSQL ='INSERT INTO students(subject,question,answer)VALUES('web lang','$ id','$ answer' )「;如果(mysqli_query($ conn,$ AnsSQL)){echo「sent」; }' –

+0

這就是完美的作品:) – MrB