问题和单选按钮循环

时间:2013-11-26 21:26:56

标签: php

我正在尝试创建一个人们可以留下有关产品的反馈的表单。大约有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";
?>

感谢

3 个答案:

答案 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循环的实现不正确。在使用前进之前,您应该检查语法。

答案 1 :(得分: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>";
}

?>

答案 2 :(得分: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'];
?>

我没有测试过,只是尝试一下让我知道