根据数据库中的值检查复选框

时间:2014-02-24 11:24:37

标签: php mysql checkbox kohana

我试图根据其数据库值检查一个复选框,我尝试过以下但没有任何欢乐,有什么建议吗?

我正在使用Kohana框架。

public static function defaultdistance(){
    $result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
    $distancestores = $result->as_array();
    foreach($distancestores as $distancestore)
    {
        echo 'Value: '.$distancestore['value'];
    }
}   

<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label>&nbsp;</label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>

2 个答案:

答案 0 :(得分:0)

可能你需要这样

$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
    ?> 
    <label class="shortLabel">
          <input type="radio" name="distance" value="10" 
         <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 
         10 <?php echo $dict->transl('distance_km'); ?> 
    </label>
    <?php 
} 

答案 1 :(得分:0)

试试这个我认为你必须使用三元运算符来解决这个问题。

<input type="radio" name="distance" value="10"
    <?php $select = $distancestore['value']=='10'?'checked':'';
    echo $select; ?>
     10 <?php echo $dict->transl('distance_km'); ?>
相关问题