繁体   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