我正在尝试编写一个自动选择所选用户的php脚本(复选框) 我不知道我错过了什么,也许你们可以帮忙。我在想的是:
$get_users = $mysqli->query("SELECT * FROM users WHERE user_type='5' AND user_id_parent='". get_uid() ."'");
$get_checkedusers = $mysqli->query("SELECT * FROM modules_users_availability WHERE module_id='". $mid ."' AND shopid='". get_uid() ."'
");
while ($row = $getcheckedusers->fetch_assoc()) {
$chosenCategory[] = $row['userid'];
}
while($users = $getusers->fetch_assoc()){
foreach($chosenCategory as $chosencategories){
if($users['user_id'] == $chosenCategory){
echo $checked = "checked";
}
}?>
<tr>
<td><input type="checkbox" <?php echo $selected; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
<td><?php echo $users['name']; ?></td>
<td><?php echo $users['email']; ?></td>
</tr>
<?php } ?>
我的问题是我无法在getusers中正确使用getcheckedusers数组。 如果有3个用户,则返回trible结果,如果是2,则返回double结果。也许我错过了什么? :)
答案 0 :(得分:1)
while($users = $getusers->fetch_assoc()){
$checked="";
if(in_array($users['user_id'],$chosenCategory)){
$checked = "checked";
}?>
<tr>
<td><input type="checkbox" <?php echo $checked; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
<td><?php echo $users['name']; ?></td>
<td><?php echo $users['email']; ?></td>
</tr>
<?php } ?>
答案 1 :(得分:0)
如果条件错误..我希望它应该喜欢这个
if ($users['user_id'] == $chosencategories) {}