Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将列字段中的值与列行中的值进行比较,并打印为选中和未选中复选框_Php_Mysql_Checkbox_Compare - Fatal编程技术网

Php 将列字段中的值与列行中的值进行比较,并打印为选中和未选中复选框

Php 将列字段中的值与列行中的值进行比较,并打印为选中和未选中复选框,php,mysql,checkbox,compare,Php,Mysql,Checkbox,Compare,我有一个带有2个表的简单数据库: 1. active_services 2. devices ------------------ ------------------------------ id | service id | active_services_value ------------------ ------------------------------ 1 | AD

我有一个带有2个表的简单数据库:

1. active_services            2. devices
------------------           ------------------------------
id   |  service                id | active_services_value
------------------           ------------------------------
1    | AD                      1  |  AD,DNS,DHCP
2    | FTP                     2  |  FTP,SMB
3    | DNS                     3  |  FTP
4    | DHCP                    4  |
5    | SMB                     5  |  AD Backup
6    | AD Backup
我使用active_services中service列的值作为表单的复选框,该表单将把选中的值发布到表设备active_service_值列。我的代码如下所示:

<?php

if($active_services_value != ""){   

// Need to generate a list of checkboxes from active_services but
// for each value in the active_services_field coresponding
// to the searched id (e.g for id 1, value1 = AD, value2 = DNS, 
// value3 = DHCP ) set the checkboxes as checked            


} else {  
// display the service in active_services as checkboxes

$q = "SELECT * from active_services";
$r = mysqli_query($dbc, $q);            
    while($list = mysqli_fetch_assoc($r)){

    ?>              

<div class="col-xs-4"><input  type="checkbox" name="checkboxvar[]" value="<?php echo $list['service']; ?>"><?php echo $list['service'];?>
</div>
<?php   }   } ?> 
</div>

?>

?>
我的问题是:

如何显示“已选中”复选框,让用户可以取消选中某些复选框,同时也显示其余未选中的复选框,再次让用户可以选中新选项

提前感谢您的帮助。

您可以通过将属性添加到输入字段来标记复选框“已选中”。因此,如果从数据库中检索所选项目的列表,则可以使用这些列表来确定是否应将checked属性添加到输入字段

它会变成这样:

<?php
$stmt = $dbc->prepare("SELECT active_services_value FROM devices WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($serviceList);
$stmt->fetch();

//transform the list of services into an array
$services = explode($serviceList, ",");

$q = "SELECT * from active_services";
$r = mysqli_query($dbc, $q);            
while($list = mysqli_fetch_assoc($r)){

?>              

<div class="col-xs-4">
    <input type="checkbox" name="checkboxvar[]"
           value="<?php echo $list['service'];?>"
           <?php if (in_array($list['service'], $services) { echo "checked"; } ?>>
    <?php echo $list['service'];?>
</div>
<?php   }   } ?> 
</div>


简单。请参阅规范化。正如@草莓所提到的,您应该规范化您的表(尤其是设备),以:1-简化数据操作,2-提高性能。如果服务列表很小并且是固定的(如示例中所示),那么您可以存储一个位值来代替它。@谢谢,您关于规范化表的想法是正确的。这是我将来所有项目都要做的事情。然而,对于当前的一个,因为它已经是一个“生产环境”,我不能做太多。目前,我只是在数据库中添加了一些新选项和一些脚本,但仅此而已……感谢您提供的代码,只需进行一些更改以匹配我的脚本,它就可以完美地工作。我会记住你对未来项目的建议。再次感谢。关于性能的观点有点误解,但是关于数据完整性的观点是正确的。