如果复选框未选中,则不显示复选框值


Do not show checkbox value if checkbox not checked

我有下面的代码,用于显示一些自定义字段。

<table>
    <tbody>
<?php 
foreach ($fields as $field) {
$type = $field['s_type'];
$label = $field['s_label'];
$value = Attributes::newInstance()->getValue($item_id, $field['pk_id']);
if ($type == 'checkbox') {
    if ($value == 'checked') $value = 'Yes';
    else $value = 'No';
}
?>
        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
<?php } ?>
    </tbody>
</table>

当复选框值= 'No'时,我需要不显示tr中的内容

这可能吗?

谢谢你的帮助!

为什么不在if语句中包装tr ?您可以重用相同的$value变量,因为您只想显示值为"Yes"时的值。

<?php
    if ($value == 'Yes')
    {
?>
        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
<?php
    }
?>

try this

 <table>
    <tbody>
<?php 
foreach ($fields as $field) {
   $displayflag=true; //<--here
$type = $field['s_type'];
$label = $field['s_label'];
$value = Attributes::newInstance()->getValue($item_id, $field['pk_id']);
if ($type == 'checkbox') {
    if ($value == 'checked'){ $value = 'Yes'; }
    else{ $value = 'No';$displayflag=false}; //<--here
}
?>
 <?php if($displayflag){?> //<--here
        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
  <?php }?>//<--here
<?php } ?>
    </tbody>
</table>