2013-03-27 204 views
-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> 

我需要不显示在TR内容时复选框值=“没有”

是否有可能做到这一点?

感谢您的帮助!

+0

不显示什么内容? – 2013-03-27 12:54:52

+0

tr。的内容现在它返回“Label1:是的,Label2:不,Label3:No”,但我只想显示“Label1:是”,而不是其他的“No”值 – GeorgeV 2013-03-27 13:00:42

+0

为什么不只是将表行包装在if语句中确定它是否有价值? – Mark 2013-03-27 13:10:16

回答

1

为什么不包裹tr在if语句?您可以重复使用相同的$value变量,因为您只想在“是”时显示该值。

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

感谢代码...我改变了!=='不',因为我也有不同的字段类型 – GeorgeV 2013-03-27 17:35:56

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>