php - 如何根据行值 1、2、5、6 恢复选中和未选中的复选框

标签 php mysql checkbox

我有一个表,其中有一列名为“角色”,以这种方式指示人员的角色“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/

相关文章:

phpmyadmin 用户名和密码

MySQL 行数

mysql - 如何删除MySQL中重复的​​连接表记录?

python - Tkinter IntVar 返回 PY_VAR0 而不是值

php - 限制每秒/分钟发送联系表单电子邮件

php - 使用 PHP 和 MySQL 开发 'Quiz' Web 应用程序的数据库设计

php、jquery问题: google chrome?

sql - (mySQL) 无法正确查询 2 个表的数据

php - 从 mysql 表中预选一组复选框

html - 为什么 HTML 复选框比其他复选框多消耗一个像素?