我想通过从数据库中提取问题及其选项来生成问题文件。存在3个问题,其中15个选项,5对于每个问题,我只能从15个中选择一个选项。下面的代码和图像输出也
<?php
for($i=1;$i<=3;$i++)
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {?>
<form action = "CHECK.php" method="POST">
<input type="radio" name="choice" value="<?php $row['OPTION1']; ?>" /> <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice" value="<?php $row['OPTION2']; ?>" /> <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice" value="<?php $row['OPTION3']; ?>" /> <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice" value="<?php $row['OPTION4']; ?>" /> <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice" value="<?php $row['OPTION5']; ?>" /> <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
答案 0 :(得分:0)
<form action = "CHECK.php" method="POST">
<?php
for($i=1;$i<=3;$i++) { ?>
<label>Question</label>
<input type="radio" name="choice<?php echo $i?>" value="a" /> 1
<input type="radio" name="choice<?php echo $i?>" value="b" /> 1
<input type="radio" name="choice<?php echo $i?>" value="c" /> 1
<input type="radio" name="choice<?php echo $i?>" value="d" /> 1
<input type="radio" name="choice<?php echo $i?>" value="e" /> 1
<br>
<?php } ?>
</form>
答案 1 :(得分:0)
这是您的操作方法。您必须将<form>
标记放在循环之外,并且必须为name
按钮设置不同的radio
属性:
<form action = "CHECK.php" method="POST">
<?php
for($i=1;$i<=3;$i++)
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION1']; ?>" /> <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION2']; ?>" /> <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION3']; ?>" /> <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION4']; ?>" /> <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION5']; ?>" /> <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
</form>
答案 2 :(得分:0)
<?php for($i=1;$i<=3;$i++)
{?>
<form action = "CHECK.php" method="POST">
<?php
$s = "select OPTION1,OPTION2,OPTION3,OPTION4,OPTION5,CORRECT_ANSWER from T_QUESTIONS where QUES_NO='$i'";
$result=mysqli_query($conn,$s);
if(!mysqli_query($conn,$s))
echo mysqli_error($conn);
else
while ($row = $result->fetch_assoc()) {?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION1']; ?>" /> <?php echo $row['OPTION1']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION2']; ?>" /> <?php echo $row['OPTION2']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION3']; ?>" /> <?php echo $row['OPTION3']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION4']; ?>" /> <?php echo $row['OPTION4']."<br>"; ?>
<input type="radio" name="choice<?php echo $i; ?>" value="<?php echo $row['OPTION5']; ?>" /> <?php echo $row['OPTION5']."<br>"; ?>
<br>
<?php
}
}
?>
</form>