在Codeigniter中设置单选按钮的值

Jen*_*enz 1 php codeigniter radio-button

在我的表单编辑细节,我有值的单选按钮01.我想要的是,在加载表单时,即当用户点击edit链接时,单选按钮应设置为数据库中的值.用户更改并提交表单后,应保留提交的值.这怎么可能?

我尝试使用下面的代码,但它不起作用.我知道不可能这样做.$row->videotype拥有数据库的价值.

<input type="radio" name="trailer" value="1" <?php 
    if(set_radio('trailer', '1', TRUE)) {
        echo set_radio('trailer', '1', TRUE); 
    } else { 
        if($row->videotype==1) ?> checked=checked <?php 
    } ?> />Yes
<input type="radio" name="trailer" value="0" <?php 
    if(set_radio('trailer', '0')) {
        echo set_radio('trailer', '0');  
    } else {
        if($row->videotype==0) ?> checked=checked <?php 
    } ?> />No
Run Code Online (Sandbox Code Playgroud)

在表单提交功能(在控制器中),我已将其设置为表单验证:

$this->form_validation->set_rules('trailer', 'Trailer', 'is_numeric');  
Run Code Online (Sandbox Code Playgroud)

任何人都可以帮我解决这个问题吗?提前致谢.

Gir*_*ish 6

set_value功能在编辑表单验证时非常有用,请尝试这种方式

<input type="radio" name="trailer" value="1" <?php 
    echo set_value('trailer', $row->videotype) == 1 ? "checked" : ""; 
?> />Yes

<input type="radio" name="trailer" value="0" <?php 
    echo set_value('trailer', $row->videotype) == 0 ? "checked" : ""; 
?> />No
Run Code Online (Sandbox Code Playgroud)

在控制器中验证规则正常,如果发布数据有空格,请使用修剪规则

$this->form_validation->set_rules('trailer', 'Trailer', 'trim|is_numeric');  
Run Code Online (Sandbox Code Playgroud)

编辑:set_radio功能创建问题在编辑模式表单验证请使用set_value功能