数组比较,实现相同值单选按钮选中效果,下面是错误代码 哪位大侠给指点下

function get_usercartype(){
global $DB;
$memberlist = $DB->fetch_one_array("select * from `partner` where `username` = '$username' ");
$arr = $memberlist['other'];
$typelist = explode("|",$arr);//从用户表中获取到选择车型的数组(有值)
//zu lin che xing
$carlist = $DB->getall("select * from `car_type`");//车型表
foreach ($carlist as $car){
$cartype[] = $car['type'];
}
foreach ($cartype as $key=>$val){
// debug($cartype[$key]);
// debug($typelist[$key]);
$str = '<td width="30" height="23" align="center" valign="middle"><input type="checkbox" name="type[]" id="type[]" value='.$cartype[$key];
if( in_array($typelist[$key],$cartype))$str .=' checked = "checked"';
$str .= ">".$cartype[$key]."</td>";
debug($str); 
}
}
//结果是哪个也没选中 那位高手给指点下

作者: wxjlogo   发布时间: 2011-06-10

if( in_array($typelist[$key],$cartype))$str .=' checked = "checked"';

中的
$typelist = explode("|",$arr);//从用户表中获取到选择车型的数组(有值)
$cartype[] = $car['type']; //从数据库得到的数组
foreach ($cartype as $key=>$val){//这个 $key 是 $cartype 的下标,能和 $typelist 的下标对应吗?

正确的写法是
if( in_array($val,$typelist))$str .=' checked = "checked"';

if( in_array($cartype[$key],$typelist))$str .=' checked = "checked"';

作者: xuzuning   发布时间: 2011-06-10

刚才试了 可是还是没有显示选中的车型

作者: wxjlogo   发布时间: 2011-06-10