我从我的数据库中回显单选按钮的值,以这种形式(一个while循环(是获取它们的最快方法。当我这样做时,它看到所有单选按钮都在同一个"组"中。如何使它有多个"组",以便可以回答多个问题?
我尝试向其添加div,但这不起作用。
<form action="Antwoord.php" method="POST">
<?php
$sql = "SELECT * FROM questionlist_choice";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender' value='$vraagF'> $vraagF<br>";
echo "</div>";
}
}
?>
<input type="submit">
</form>
抱歉,我不太了解您的问题,但是如果您想GROUP
它们,则需要像这样name
标签上添加索引:
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
$vraag = $row['Vraag'];
$vraagA = $row['Vraag_keuzeA'];
$vraagB = $row['Vraag_keuzeB'];
$vraagC = $row['Vraag_keuzeC'];
$vraagD = $row['Vraag_keuzeD'];
$vraagE = $row['Vraag_keuzeE'];
$vraagF = $row['Vraag_keuzeF'];
echo "<div>";
echo "<br><p>$vraag</p>";
echo "<input type='radio' name='gender[$i]' value='$vraagA'> $vraagA<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagB'> $vraagB<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagC'> $vraagC<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagD'> $vraagD<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagE'> $vraagE<br>";
echo "<input type='radio' name='gender[$i]' value='$vraagF'> $vraagF<br>";
echo "</div>";
$i++;
}
我使用$i
作为索引,我认为如果你使用for loop
会更好,因为它已经具有$key
价值,我认为在while
使用它是一个坏习惯。