日期:2014-05-17 浏览次数:20461 次
<?php
$arr = array(
0 => array('id'=>12,'name'=>'NO.1'),
1 => array('id'=>13,'name'=>'NO.2'),
);
$_check_id = 0;
if($arr) {
foreach($arr as $v){
?>
<input type="radio" name="c" id="d_<?php echo $v['id']?>" value="<?php echo $v[id]?>" /><?php echo $v['name']?><br>
<?php
}
}?>
$arr = array(
0 => array('id'=>12,'name'=>'NO.1'),
1 => array('id'=>13,'name'=>'NO.2'),
2 => array('id'=>14,'name'=>'NO.3'),
3 => array('id'=>15,'name'=>'NO.4'),
);
$_check_id = 14;
$found = false;
$htmls = array();
if($arr) {
foreach($arr as $v){
$input_pre = <<<EOT
<input type="radio" name="c" id="d_{$v['id']}" value="{$v['id']}"
EOT;
$input_post = <<<EOT
/>{$v['name']}
EOT;
if(!$found && $_check_id == $v['id']) {
$found = true;
$input_pre .= ' checked ';
}
$htmls[] = array($input_pre, $input_post);
}
}
$htmlstr = '';
foreach($htmls as $html) {
if(!$found) {
$html[0] .= ' checked ';
$found = true;
}
$htmlstr .= implode('', $html);
}
echo $htmlstr;
<?php
$arr = array(
0 => array('id'=>12,'name'=>'NO.1'),
1 => array('id'=>13,'name'=>'NO.2'),
2 => array('id'=>14,'name'=>'NO.3'),
);
$_check_id = 0;
if($arr) {
foreach($arr as &$v) {
if($_check_id == $v['id']) {
$v['check'] = ' checked';
$_check_id = -1;
}
}
if($_check_id >=0) $arr[0]['check'] = ' checked';
foreach($arr as $v){
?>
<input type="radio" name="c" id="d_<?php echo $v['id']?>" value="<?php echo $v['id']?>" <?php ;echo isset($v['check'])?$v['check']:''?> /><?php echo $v['name']?>
<?php
}
}?>
foreach($arr as $key=>$v){
if($key == $_check_id){
//这儿是checked
continue;
}
......