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

时间:2021-07-09 22:50:52

I have a simple db with 2 tables:

我有一个简单的数据库有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

I use the values of service column in active_services as checkboxes for a form that will post the checked values to the table device, column active_service_values. My code looks like:

我使用active_services中service列的值作为表单的复选框,该表单将选中的值发布到表设备,列为active_service_values。我的代码看起来像:

<?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>

?>

My question is:

我的问题是:

How do I display the "checked" checkboxes, giving the users the possibility to uncheck some of the checkboxes, while displaying also the remaining ones, unchecked, again, giving the users the possibility to check new options?

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

Thank you all in advance for the help.

提前谢谢大家的帮助。

1 个解决方案

#1


You can mark a checkbox "checked" by adding the checked attribute to the input field. So if you retrieve the list of selected items from the database you can use those to determine if you should add the checked attribute to the input field.

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

It would become something like this:

它会变成这样的:

<?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>

I also have a few additional points for improvement for you that are somewhat related to your question:

我还有一些额外的改进点,与您的问题有些相关:

  • As others have mentioned in the comments already you should normalize your tables (i.e. not putting all active services in one column, instead use a separate table for it containing device,service pairs)
  • 正如其他人已在评论中提到的那样,您应该规范化表格(即不将所有活动服务放在一列中,而是使用包含设备,服务对的单独表格)

  • Instead of using the names of the services in the devices table you can better reference to the id of the services in the active_services table and set it up as a foreign key. That will improve performance and will guarantee consistency in your table (i.e. if set up correctly it will be impossible to use a non-existing service in your devices table).
  • 您可以更好地引用active_services表中服务的id并将其设置为外键,而不是使用devices表中的服务名称。这将提高性能并保证表中的一致性(即,如果设置正确,则无法在设备表中使用不存在的服务)。

#1


You can mark a checkbox "checked" by adding the checked attribute to the input field. So if you retrieve the list of selected items from the database you can use those to determine if you should add the checked attribute to the input field.

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

It would become something like this:

它会变成这样的:

<?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>

I also have a few additional points for improvement for you that are somewhat related to your question:

我还有一些额外的改进点,与您的问题有些相关:

  • As others have mentioned in the comments already you should normalize your tables (i.e. not putting all active services in one column, instead use a separate table for it containing device,service pairs)
  • 正如其他人已在评论中提到的那样,您应该规范化表格(即不将所有活动服务放在一列中,而是使用包含设备,服务对的单独表格)

  • Instead of using the names of the services in the devices table you can better reference to the id of the services in the active_services table and set it up as a foreign key. That will improve performance and will guarantee consistency in your table (i.e. if set up correctly it will be impossible to use a non-existing service in your devices table).
  • 您可以更好地引用active_services表中服务的id并将其设置为外键,而不是使用devices表中的服务名称。这将提高性能并保证表中的一致性(即,如果设置正确,则无法在设备表中使用不存在的服务)。