【问题标题】:Show value taken from database as a radio button显示取自数据库的值作为单选按钮
【发布时间】:2015-11-10 06:07:16
【问题描述】:

我创建了一个数据库并检索了一些值并将它们作为文本显示在我的视图中。我想将这些值显示为单选按钮。我该怎么做?

<?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 html codeigniter


【解决方案1】:

试试这个

<?php 
    foreach ($records as $rec) {
    ?>
        <input type="radio" name="" value="<?php echo $rec['ans'] ?>"><?php echo $rec['ans'] ?>
    <?php                      
    }
?>

问题应该出现在另一个数组中

【讨论】:

    【解决方案2】:

    你可以这样做:

    <?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/>";
    
    }
    ?>
    

    【讨论】:

    • 所以如果数组是 4 那么你得到 4*4=16 个答案
    • 自己试试。毫无疑问,您将被否决。不是我
    • 如果问题和可能的 4 个答案在一个数据库对象中,为什么有人需要再次循环它?参考:有问题的代码
    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-15
    • 1970-01-01
    相关资源
    最近更新 更多