我一直想从数据库中显示问题和答案,并将其显示为普通问卷。
但是,我只能显示最后一行。是否需要更改代码?
<?php
for ($i = 1; $i <= ($row = mysqli_fetch_assoc($result)); $i++) {
$question = $row['question'];
$option1 = $row['option1'];
$option2 = $row['option2'];
$option3 = $row['option3'];
?>
<?php echo $question ?> <br>
<input type="radio" name="$i" value="$option1"><?php echo $option1 ?><br>
<input type="radio" name="$i" value="$option2"><?php echo $option2 ?><br>
<input type="radio" name="$i" value="$option3"><?php echo $option3 ?><br>
<input type="submit" value="Submit"/>
<?php
}
?>
感谢。
答案 0 :(得分:1)
尝试使用while循环:
<?php
$count = 0;
while ($row = mysqli_fetch_assoc($result)) {
$count++;
$question = $row['question'];
$option1 = $row['option1'];
$option2 = $row['option2'];
$option3 = $row['option3'];
?>
<?=$question?><br />
<input type="radio" name="<?=$count?>" value="<?=$option1?>"><?=$option1?><br />
<input type="radio" name="<?=$count?>" value="<?=$option2?>"><?=$option2?><br />
<input type="radio" name="<?=$count?>" value="<?=$option3?>"><?=$option3?><br />
<input type="submit" value="Submit"/>
<?php
}
?>