簡體   English   中英

問題和單選按鈕循環

[英]questions and radio buttons loop

我正在嘗試創建一個表單,使人們可以留下有關該產品的反饋。 大約有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";
?>

謝謝

如果您需要遍歷數組以生成單選表單,則可以執行以下操作:

$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循環的實現不正確。 在使用該方法之前,您應該檢查語法。

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

?>

有問題的單選按鈕

 <?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'];
?>

我尚未測試,請嘗試一下,讓我知道

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM