我创建了一个数据库,检索了一些值,并将它们作为文本显示在视图中。我想将这些值显示为单选按钮。我该怎么做?
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo $rec->ans1."<br/>";
echo $rec->ans2."<br/>";
echo $rec->ans3."<br/>";
echo $rec->ans4."<br/>";
}
?>
像这样,我在数据库中以文本形式显示了我的价值观。我怎样才能像单选按钮一样显示这些?
谢谢。
试试这个
<?php
foreach ($records as $rec) {
?>
<input type="radio" name="" value="<?php echo $rec['ans'] ?>"><?php echo $rec['ans'] ?>
<?php
}
?>
问题应该出现在另一个阵列中
您可以按如下方式执行此操作:
<?php
foreach ($records as $rec) {
echo $rec->id."). ";
echo $rec->question."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans1_".$rec->id."'>".$rec->ans1."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans2_".$rec->id."'>".$rec->ans2."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans3_".$rec->id."'>".$rec->ans3."<br/>";
echo "<input type='radio' name='question_".$rec->id."' value='ans4_".$rec->id."'>".$rec->ans4."<br/>";
}
?>