2013-11-26 104 views
0

我試圖創建一個表單,人們可以留下有關產品的反饋。有大約10個問題...也許增加..我想用循環這個單選按鈕。而不是爲每個問題創建6個新的單選按鈕。種類卡住了。這裏的代碼... 還有我如何使用循環從這個頁面收集結果到下一頁的任何幫助? stackoverflow不允許我粘貼這裏的代碼...與縮進有關。花了半個小時,並不能解決什麼是錯的哈哈。 所以我粘貼在這裏的代碼,問題和單選按鈕循環

<?php 
$questions = array(
    ("Question 1 - What did you think of the product?", "Question 2 - Would you use it again?", "Question 2 - How likely will you recommend this product to your friends/family?" 

); 

?> 

<?php 

    for ($questions = 0; $questions <= 3; ++$i) { 
     $echo .$questions and .$i 
    } 
?> 


<?php 
    for ($i = 0; $questions <= 3; ++$i) { 
     $questions[] = $i; 
    } 
"<input type='radio' name='Question[]' value='6'>6"; 
"<input type='radio' name='Question[]' value='5'>5"; 
"<input type='radio' name='Question[]' value='4'>4"; 
"<input type='radio' name='Question[]' value='3'>3"; 
"<input type='radio' name='Question{]' value='2'>2"; 
"<input type='radio' name='Question[]' value='1'>1"; 
?> 

感謝

回答

0

與問題

<?php 
    try 
    { 
    //Storing no. of Total Questions for next page in session 
    session_start(); 
    $ttlque = count($questions); 
    $_SESSION['ttlquestions'] = $ttlque; 
    for ($i = 0; $i < $ttlque; $i++) 
    { 
    echo $questions[$i].'<br /><label><input type="radio" name="Question'.$i.'" value="6">6</label><br /><label><input type="radio" name="Question'.$i.'" value="5">5</label><br /><label><input type="radio" name="Question'.$i.'" value="4">4</label><br /><label><input type="radio" name="Question'.$i.'" value="3">3</label><br /><label><input type="radio" name="Question'.$i.'" value="2">2</label><br /><label><input type="radio" name="Question'.$i.'" value="1">1</label>'; 
    } 
    } 
    catch(Exception $e) 
    { 
     echo 'Message: ' .$e->getMessage(); 
    } 
?> 
結果生成的頁面上

單選按鈕爲此

<?php 
    $answer = 0; 
    for($i = 0; $i<$_SESSION['ttlquestions']; $i++) 
    { 
     $answer += $_POST['Question'.$i]; 
    } 
    echo $answer/$_SESSION['ttlquestions']; 
?> 

我沒有帶測試,只是給它一個嘗試,讓我知道

+0

非常感謝!終於搞定了。我會添加更多的問題並完成此。我將創建一個數據庫(phpMyAdmin)會session_start與它正常工作嗎?我曾經使用foreach問題...得到結果 – user3018797

+0

它只允許我在整個頁面中選擇一個單選按鈕。它應該允許我在一個問題中從6中選擇一個單選按鈕。 – user3018797

+0

好吧,讓我改變代碼 –

0

如果您需要通過數組循環並生成無線形式,你可以做這樣的事情:

$questions = array(
    1 => "Question 1 - What did you think of the product?", 
    2 => "Question 2 - Would you use it again?", 
    3 => "Question 3 - How likely would you recommend this product?" 
); 

foreach ($questions as $k => $v) { 
    echo "<input type='radio' name='Question".$k."' value='".$k."' />"." ".$v."<br>"; 
} 

當與處理數組最好使用foreach循環,因爲它遍歷數組。此外,你的FOR循環的實現是不正確的。在使用前進之前,你應該檢查你的語法。

0
<?php 
$questions = array(
    "Question 1 - What did you think of the product?", 
    "Question 2 - Would you use it again?", 
    "Question 2 - How likely will you recommend this product to your friends/family?" 
); 
// You had the right idea... Just use a different variable (since "questions" 
// has already been used). Also, you can have PHP just count the questions 
// for you instead of hard-coding "3": 
for ($q = 0; $q <= count($questions); $q++) { 
    // I assume "$people" has been defined somewhere else...? 
    // No $ in front of "echo" 
    // A dot "." means "and" (kind of). 
    echo $people . $q; 
} 

// Different variable other than $people 
// Less than as opposed less than or equal to (arrays start at 0 but the count starts at 
// 1 just like anything else you count), but we'll start $i as 1 so we have to up the 
// counted value by adding 1 
$count_questions = count($questions) + 1; 
for ($i = 1; $i < $count_questions; $i++) { 
    // Remember to close input tags 
    echo "<input type='radio' name='Question[]' value='$i'>$i</input>"; 
} 

?>