错误:如果没有重复相同的复选框超过 1 次,我无法显示处于选中状态的复选框
Erro: I cannot show the checkboxes in checked state without the same checkbox being repeated more than 1 time
Table结构
<?php
//the grupo_usuarios table contains the database user groups
CREATE TABLE `grupo_usuarios` (
`id` int(11) NOT NULL AUTOINCREMENT,
`nombre` varchar(20) COLLATE utf8_spanish_ci NOT NULL,
`descripcion` varchar(150) COLLATE utf8_spanish_ci NOT NULL
);
//the permisos table contains the permissions
CREATE TABLE `permisos` (
`id` int(11) PRIMARY KEY NOT NULL AUTOINCREMENT,
`nombre` varchar(20) COLLATE utf8_spanish_ci NOT NULL
);
// the table tiene_asignado is the result of the many-to-many relationship between the grupo_usuarios table and permisos
CREATE TABLE `tiene_asignado` (
`id` int(11) PRIMARY KEY NOT NULL AUTOINCREMENT,
`id_grupo` int(11) NOT NULL,
`id_permisos` int(11) NOT NULL,
FOREIGN KEY(id_grupo) REFERENCES grupo_usuarios(id),
FOREIGN KEY(id_permisos) REFERENCES permisos(id),
ON DELETE CASCADE ON UPDATE CASCADE
);
?>
<?php include('php/verificarSesion.php');?>
<?php
//Storing the id of the user group received through GET from the groups.php page
if(isset($_GET['id_grupo_usuarios']) && (!empty($_GET['id_grupo_usuarios'])) && is_numeric($_GET['id_grupo_usuarios']) == 1){
$id_grupo_usuarios = $_GET['id_grupo_usuarios'];
}else{
echo "<script>
window.location.href='verGrupo.php';
</script>";}
?>
<?php include("plantillas/header.php"); ?>
<!-- PAGE CONTENT (permisosChex.php)-->
<section class="listadoPacientes">
<div class="container mt-3">
<div class="row">
<div class="col-6 col-md-5">
<h2>Assign permissions to the group <?php echo $id_grupo_usuarios;?></h2>
<p>Here you can check the permissions belonging to this group</p>
</div>
<div class="col-4" id="tabla">
<table id="tablaPacientes" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Permissions</th>
</tr>
</thead>
<tbody>
<!--START OF THE DATA SUBMISSION FORM-->
<form class="row formularioCrearPaciente" action="php/asignarPermisos.php" method="post" id="FormularioActualizarPaciente">
<input type="text" value="<?php echo $id_grupo_usuarios; ?>" name="id_grupo_usuarios">
<?php include("php/conexion.php")?>
<?php
//query to display all permissions
$sql = "SELECT * FROM permisos ORDER BY id DESC";
$resultado = $conexion->query($sql);
$listadoPermisos = array('data' => array());
if($resultado->num_rows > 0) {
//we will show a numbering in the table>>>>numbering: 1, 2, 3....
$numeracion = 0;
while($fila = $resultado->fetch_array()) {
$numeracion = $numeracion + 1;
//the permit id is stored here
$id_permiso = $fila[0];
$nombre = $fila['nombre'];
在第二个查询中,我获得了分配给一组用户的权限
$consulta2 = "SELECT id_permisos FROM tiene_asignado where id_grupo = $id_grupo_usuarios";
$resultados = $conexion->query($consulta2);
while( $asignados = $resultados->fetch_array()){
//here I store the permissions of the selected checkboxes
$datos=array();
if (is_array($asignados) == true) {
//I assign a variable for each selected checkbox in the array
foreach($asignados as $asignado)
{
我将从复选框中选择的权限与 table (tiene_asignado)
的 id_permisos 字段相关联
$datos[$asignados['id_permisos']] = true;
}
}else{//END OF is_array($asignados)
//this is just a test
echo "nothing";
}
?>
<tr>
<td>
<?php echo $numeracion;?>
<input title='create sheet of <?php echo $id_grupo_usuarios?>' type="checkbox" name="permisos[]" value="<?php echo $id_permiso;?>" class="delete-checkbox" <?php echo isset($datos[$fila[0]]) ? 'checked' : '';?> >
</tr
当我在第二个 WHILE 中包含输入复选框时,我只能看到分配给该组的权限如果我不这样做,问题是输入也会重复,也就是说,如果组分配了 3 个权限,每个输入都重复了 3 次,但它显示了分配给该组的 3 个权限
<?php } //END OF THE SECOND WHILE?>
<?php } //END OF THE FIRST WHILE?>
<?php
} //END OF IF?>
<?php ?>
<div class="col-md-6 form-group">
<button type="submit"id="ActualizarPaciente" class="btn btn-primary">Asignar Permisos</button>
</div>
</form>
<?php
?>
</div>
</div>
</div>
</tbody>
</table>
</div>
</div>
</div>
</section>
<!-- END OF PAGE CONTENT -->
<!-- Including the footer of the page -->
<?php include('plantillas/footer.php'); ?>
<!-- script js which contains the functionalities of the permission list page -->
<!--<script src="js/verpermisosChex.js"></script>-->
您正在 select 搜索 permisos
中的所有记录,对于每条记录,您 select 匹配 id_grupo
中的 tiene_asignado
中的所有记录一个值。但是,很明显,您的 permisos
table 通过 id_permisos
字段在逻辑上与 tiene_asignado
相关。
那么,让我们看一下您的第二个查询:它仅从 tiene_asignado
加载 id_permisos
并且可以安全地假设您的 permisos
table 有一个 ID,可能称为 id_permisos
,可能具有不同的名称,并且您已经使用第一个查询加载了 id_permisos
值。
因此,您的第二个查询似乎是不必要的,因此,您需要删除第二个查询和第二个while
(当然,您需要第二个[=22=的内容],但它不需要是一个循环)。如果出于某种原因您需要从 tiene_asignado
加载,那么您可以将第一个查询修改为 join
。但是,如果您在修改第一个查询时需要帮助,则需要提供有关您的 table 的信息,至少是它们的字段。
编辑
根据最初撰写此答案后发现的后续信息,我建议使用以下查询:
$sql = "SELECT * FROM permisos p LEFT JOIN tiene_asignado ta ON p.id = ta.id_permisos AND id_grupo='".$id_grupo_usuarios."' ";)
Table结构
<?php
//the grupo_usuarios table contains the database user groups
CREATE TABLE `grupo_usuarios` (
`id` int(11) NOT NULL AUTOINCREMENT,
`nombre` varchar(20) COLLATE utf8_spanish_ci NOT NULL,
`descripcion` varchar(150) COLLATE utf8_spanish_ci NOT NULL
);
//the permisos table contains the permissions
CREATE TABLE `permisos` (
`id` int(11) PRIMARY KEY NOT NULL AUTOINCREMENT,
`nombre` varchar(20) COLLATE utf8_spanish_ci NOT NULL
);
// the table tiene_asignado is the result of the many-to-many relationship between the grupo_usuarios table and permisos
CREATE TABLE `tiene_asignado` (
`id` int(11) PRIMARY KEY NOT NULL AUTOINCREMENT,
`id_grupo` int(11) NOT NULL,
`id_permisos` int(11) NOT NULL,
FOREIGN KEY(id_grupo) REFERENCES grupo_usuarios(id),
FOREIGN KEY(id_permisos) REFERENCES permisos(id),
ON DELETE CASCADE ON UPDATE CASCADE
);
?>
<?php include('php/verificarSesion.php');?>
<?php
//Storing the id of the user group received through GET from the groups.php page
if(isset($_GET['id_grupo_usuarios']) && (!empty($_GET['id_grupo_usuarios'])) && is_numeric($_GET['id_grupo_usuarios']) == 1){
$id_grupo_usuarios = $_GET['id_grupo_usuarios'];
}else{
echo "<script>
window.location.href='verGrupo.php';
</script>";}
?>
<?php include("plantillas/header.php"); ?>
<!-- PAGE CONTENT (permisosChex.php)-->
<section class="listadoPacientes">
<div class="container mt-3">
<div class="row">
<div class="col-6 col-md-5">
<h2>Assign permissions to the group <?php echo $id_grupo_usuarios;?></h2>
<p>Here you can check the permissions belonging to this group</p>
</div>
<div class="col-4" id="tabla">
<table id="tablaPacientes" class="table table-bordered table-striped" style="width:100%">
<thead>
<tr>
<th>Permissions</th>
</tr>
</thead>
<tbody>
<!--START OF THE DATA SUBMISSION FORM-->
<form class="row formularioCrearPaciente" action="php/asignarPermisos.php" method="post" id="FormularioActualizarPaciente">
<input type="text" value="<?php echo $id_grupo_usuarios; ?>" name="id_grupo_usuarios">
<?php include("php/conexion.php")?>
<?php
//query to display all permissions
$sql = "SELECT * FROM permisos ORDER BY id DESC";
$resultado = $conexion->query($sql);
$listadoPermisos = array('data' => array());
if($resultado->num_rows > 0) {
//we will show a numbering in the table>>>>numbering: 1, 2, 3....
$numeracion = 0;
while($fila = $resultado->fetch_array()) {
$numeracion = $numeracion + 1;
//the permit id is stored here
$id_permiso = $fila[0];
$nombre = $fila['nombre'];
在第二个查询中,我获得了分配给一组用户的权限
$consulta2 = "SELECT id_permisos FROM tiene_asignado where id_grupo = $id_grupo_usuarios";
$resultados = $conexion->query($consulta2);
while( $asignados = $resultados->fetch_array()){
//here I store the permissions of the selected checkboxes
$datos=array();
if (is_array($asignados) == true) {
//I assign a variable for each selected checkbox in the array
foreach($asignados as $asignado)
{
我将从复选框中选择的权限与 table (tiene_asignado)
的 id_permisos 字段相关联$datos[$asignados['id_permisos']] = true;
}
}else{//END OF is_array($asignados)
//this is just a test
echo "nothing";
}
?>
<tr>
<td>
<?php echo $numeracion;?>
<input title='create sheet of <?php echo $id_grupo_usuarios?>' type="checkbox" name="permisos[]" value="<?php echo $id_permiso;?>" class="delete-checkbox" <?php echo isset($datos[$fila[0]]) ? 'checked' : '';?> >
</tr
当我在第二个 WHILE 中包含输入复选框时,我只能看到分配给该组的权限如果我不这样做,问题是输入也会重复,也就是说,如果组分配了 3 个权限,每个输入都重复了 3 次,但它显示了分配给该组的 3 个权限
<?php } //END OF THE SECOND WHILE?>
<?php } //END OF THE FIRST WHILE?>
<?php
} //END OF IF?>
<?php ?>
<div class="col-md-6 form-group">
<button type="submit"id="ActualizarPaciente" class="btn btn-primary">Asignar Permisos</button>
</div>
</form>
<?php
?>
</div>
</div>
</div>
</tbody>
</table>
</div>
</div>
</div>
</section>
<!-- END OF PAGE CONTENT -->
<!-- Including the footer of the page -->
<?php include('plantillas/footer.php'); ?>
<!-- script js which contains the functionalities of the permission list page -->
<!--<script src="js/verpermisosChex.js"></script>-->
您正在 select 搜索 permisos
中的所有记录,对于每条记录,您 select 匹配 id_grupo
中的 tiene_asignado
中的所有记录一个值。但是,很明显,您的 permisos
table 通过 id_permisos
字段在逻辑上与 tiene_asignado
相关。
那么,让我们看一下您的第二个查询:它仅从 tiene_asignado
加载 id_permisos
并且可以安全地假设您的 permisos
table 有一个 ID,可能称为 id_permisos
,可能具有不同的名称,并且您已经使用第一个查询加载了 id_permisos
值。
因此,您的第二个查询似乎是不必要的,因此,您需要删除第二个查询和第二个while
(当然,您需要第二个[=22=的内容],但它不需要是一个循环)。如果出于某种原因您需要从 tiene_asignado
加载,那么您可以将第一个查询修改为 join
。但是,如果您在修改第一个查询时需要帮助,则需要提供有关您的 table 的信息,至少是它们的字段。
编辑
根据最初撰写此答案后发现的后续信息,我建议使用以下查询:
$sql = "SELECT * FROM permisos p LEFT JOIN tiene_asignado ta ON p.id = ta.id_permisos AND id_grupo='".$id_grupo_usuarios."' ";)