我有一个表,其中有一列名为“角色”,以这种方式指示人员的角色“1,3,6..”,以及其他具有此角色名称的表。任何 id 都对应一个角色。
我正在尝试将所有角色打印为复选框,并检查此人是否具有其中任何角色。
我正在尝试内爆和爆炸,但没有结果,你能指导我吗?
<input type="checkbox" name="roles[]" value="'.($row['id']).'" >'.($row['name']).'</input>
最佳答案
试试这个
$user_id = $_REQUEST['user_id'];
$user_role = array();
$result = mysql_query("SELECT role_id FROM user_role where user_id = '$user_id' ");
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
$user_role[] = explode(',' $row['role_id']);
}// end while
$getRoleList = mysql_query("select role_id, role_name from role ");
while ($roleRow = mysql_fetch_array($getRoleList, MYSQL_BOTH)) {
if(in_array($roleRow['role_id'], $user_role)){
echo '<input type="checkbox" name="roles[]" value="'.($roleRow['role_id']).'" checked="checked">'.($roleRow['role_name']);
}else{
echo '<input type="checkbox" name="roles[]" value="'.($roleRow['role_id']).'" >'.($roleRow['role_name']);
}// end if
}// end while
关于php - 如何根据行值 1、2、5、6 恢复选中和未选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131322/