-2

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

<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 中显示内容

有可能这样做吗?

谢谢你的帮助!

4

2 回答 2

1

尝试这个

 <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>
于 2013-03-27T13:06:42.977 回答
1

Why not wrap the tr in an if statement? You can reuse the same $value variable as you only want to show the value when it is "Yes".

<?php
    if ($value == 'Yes')
    {
?>
        <tr>
            <td class='detail_label'><?php echo $label; ?></td>
            <td class='detail_label'><?php echo $value; ?></td>
        </tr>
<?php
    }
?>
于 2013-03-27T13:18:36.977 回答