我从一列数据库中获取值,如4,5,6,7,8,存储在
中$sid=$user['std_list'];
我希望将它与另一个变量$ ctegoryy [' id']进行比较,它具有相同的值,但在不同的行4,5,6,7,8中但在不同的行中。 我的比较代码是
<?php while($ctegoryy=mysql_fetch_array($rsultt)) { ?>
<li style="text-transform: capitalize;">
<?php if($ctegoryy['id']==$user['std_list']){
?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" checked>
<?php }
else {?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" /><?php
} echo $ctegoryy['firstname']."  ".$ctegoryy['lastname'];?></li>
<?php }?>
答案 0 :(得分:0)
检查此解决方案,以便将数组值与单个变量
进行比较<?php
$sid=$user['std_list'];
//from your question its look like you are receiving value in string 4,5,6,7,8. So need to convert in array. If its in array then you can skip following step
$sid = explode(",", $sid);
while($ctegoryy=mysql_fetch_array($rsultt)) { ?>
<li style="text-transform: capitalize;">
<?php if( in_array($ctegoryy['id'] , $sid)){ //PHP in built function to check value exist in array
?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" checked>
<?php }
else {?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" /><?php
} echo $ctegoryy['firstname']."  ".$ctegoryy['lastname'];?></li>
<?php }?>
答案 1 :(得分:0)
首先获取行值并转换为数组
<?php
$access=explode(",",$row['std_list']);
if(in_array($ctegoryy['id'],$access)){ $d='t'; } else { $d='f'; }
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" <?php if($d=="t") { ?> checked="checked" <?php } ?>/>