繁体   English   中英

选中复选框,具体取决于数据库中的值

[英]Check checkbox depending on a value in database

我正在尝试根据其数据库值检查一个复选框,我尝试了以下操作,但没有任何乐趣,有什么建议吗?

我正在使用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>

可能你需要这样

$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 
} 

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM