dongyong3223
dongyong3223
2012-03-22 07:00

PHP条件语句显示一个字段

已采纳

I have inserted a code to display product attribute in view.phptml, which works fine. But it shows a empty box, even when the field is empty.

The code I used to display is like this

<div class="add-to-box">        
<div class="add-to-cart"><div>image path</div>
<h3><?php
echo $_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift')
?></h3></div></div>

How I can add a condition to above statement to hide the empty box from display when the field is empty.!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doubi4531 doubi4531 9年前

    Use either empty or is_null, like so:

    <?php
        if( !empty( $_product->getFreeGift() ) ){
    ?>
    
    ... HTML here ...
    
    <?php
        }
    ?>
    

    or

    <?php
        if( !is_null( $_product->getFreeGift() ) ){
    ?>
    
    ... HTML here ...
    
    <?php
        }
    ?>
    
    点赞 评论 复制链接分享
  • douqing0713 douqing0713 9年前

    Hi may be you can use this

    <?php 
    if (isset($_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift'))) {
    echo('<div class="add-to-box">        
             <div class="add-to-cart">
             <div>image path</div>
              <h3>'.$_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift').'</h3>
              </div>
          </div>');
    } else {
       ..... // other stuff
    }
    

    The isset function here help to know if a variable contain something usefull. good work !

    点赞 评论 复制链接分享

相关推荐