简体   繁体   English

PHP 单选按钮在第二个循环中彼此相邻

[英]PHP radio buttons next to each other in second for loop

I am trying to get radio buttons to display next to each other, but it is creating 2 lines or radio buttons instead of all 5 next to each other.我试图让单选按钮彼此相邻显示,但它正在创建 2 行或单选按钮,而不是所有 5 个彼此相邻。 $mcqnum is a value I am getting from another PHP page. $mcqnum 是我从另一个 PHP 页面获得的值。 That is working fine.那工作正常。 Its the second for loop that is not putting them next to each other, it is placing them in 2 line.它的第二个 for 循环没有将它们彼此相邻,而是将它们放在 2 行中。

enter code here

<?php
SESSION_START();
echo "Exam page";
$mcqnum = $_SESSION['nummcq'];  //Get number of MCQ ?
echo "$mcqnum";

$i = 0;
$j = 0;

for ($i = 1; $i <= $mcqnum; $i++){
    echo"<form method = 'post'> ";
     echo "$i <input type='radio' name='num' value='$i>  <br>";
     for ($j= 1; $j<5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num' value='$i' ><br>";
         echo"</label>";
          }
     echo"</form>";
}
?>

This is the output Output这是 output Output

You have multiple form constructs in your codes.您的代码中有多个表单结构。 This will cause carriage returns, and other problems.这将导致回车和其他问题。

Please move the FORM and /FORM outside the loops:请将 FORM 和 /FORM 移到循环之外:

Hence, change因此,改变

for ($i = 1; $i <= $mcqnum; $i++){
    echo"<form method = 'post'> ";
     echo "$i <input type='radio' name='num' value='$i>  <br>";
     for ($j= 1; $j<5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num' value='$i' ><br>";
         echo"</label>";
          }
     echo"</form>";
}

to

echo "<form method = 'post'> ";
for ($i = 1; $i <= $mcqnum; $i++){

     echo "$i";

     for ($j= 1; $j<=5;$j++){
         echo"<label style='display:inline-block'>";
         echo "<input type='radio' name='num" . $i . "' value='$j' >";
         echo"</label>";
          }

echo "<br>";
}

     echo"</form>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM