Home >Backend Development >PHP Tutorial > 程序逻辑有有关问题,请帮忙
程序逻辑有问题,请大虾帮忙
<?php<br /> $arr = array(<br /> 0 => array('id'=>12,'name'=>'NO.1'),<br /> 1 => array('id'=>13,'name'=>'NO.2'),<br /> );<br /> <br /> $_check_id = 0;<br /> if($arr) {<br /> foreach($arr as $v){<br /> ?><br /> <input type="radio" name="c" id="d_<?php echo $v['id']?>" value="<?php echo $v[id]?>" /><?php echo $v['name']?><br><br /> <?php <br /> }<br /> }?>
<br /> <br /> $arr = array(<br /> 0 => array('id'=>12,'name'=>'NO.1'),<br /> 1 => array('id'=>13,'name'=>'NO.2'),<br /> 2 => array('id'=>14,'name'=>'NO.3'),<br /> 3 => array('id'=>15,'name'=>'NO.4'),<br /> );<br /> <br /> $_check_id = 14;<br /> $found = false;<br /> $htmls = array();<br /> if($arr) {<br /> foreach($arr as $v){<br /> $input_pre = <<<EOT<br /> <input type="radio" name="c" id="d_{$v['id']}" value="{$v['id']}"<br /> EOT;<br /> $input_post = <<<EOT<br /> />{$v['name']}<br /> EOT;<br /> if(!$found && $_check_id == $v['id']) {<br /> $found = true;<br /> $input_pre .= ' checked ';<br /> }<br /> $htmls[] = array($input_pre, $input_post);<br /> }<br /> }<br /> $htmlstr = '';<br /> foreach($htmls as $html) {<br /> if(!$found) {<br /> $html[0] .= ' checked ';<br /> $found = true;<br /> }<br /> $htmlstr .= implode('', $html);<br /> }<br /> echo $htmlstr;<br />
<?php<br /> $arr = array(<br /> 0 => array('id'=>12,'name'=>'NO.1'),<br /> 1 => array('id'=>13,'name'=>'NO.2'),<br /> 2 => array('id'=>14,'name'=>'NO.3'),<br /> );<br /> <br /> $_check_id = 0;<br /> if($arr) {<br /> foreach($arr as &$v) {<br /> if($_check_id == $v['id']) {<br /> $v['check'] = ' checked';<br /> $_check_id = -1;<br /> }<br /> }<br /> if($_check_id >=0) $arr[0]['check'] = ' checked';<br /> foreach($arr as $v){<br /> ?><br /> <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']?><br /> <?php <br /> }<br /> }?>
<br /> foreach($arr as $key=>$v){<br /> if($key == $_check_id){<br /> //这儿是checked<br /> continue;<br /> }<br /> ......<br />