如果属性为字符串(0),则不显示

时间:2011-07-06 10:21:10

标签: php string magento null

我正在使用以下代码部分在我们的Magento商店中显示一些图标,其中的想法是,如果图标部分中没有任何内容添加它不应该显示,由于某种原因这不起作用...它显示的是一个分裂,好像有什么东西,但实际上什么都没有。

<?php 
if($_helper->productAttribute($_product,($_product->geticons()), 'icons') !== null): 
?>
    <div class="product-icons">
        <?php echo $_helper->productAttribute($_product,($_product->geticons()), 'icons') ?>
    </div>
<?php endif; ?>

如果在属性字段中编码了图标,则需要显示图标,如果没有添加任何内容则隐藏分割。

我已经知道代码返回的值为string(0)我需要在编码中更改以获得所需的效果吗?

3 个答案:

答案 0 :(得分:2)

这是你需要的东西,你不需要两次调用相同的功能来获得空结果。定义您的变量并检查它是否为空(null,undefined或false)或不是

<?php $icons = $_helper->productAttribute($_product,($_product->getIcons()), 'icons');?>
<?php if(!empty($icons)):?>
    <div class="product-icons">
        <?php echo $icons;?>
    </div>
<?php endif;?>

这可能是更好的解决方案,因为它不会调用帮助程序,除非已定义图标但您首先必须在代码库上尝试它。

<?php if($_product->getIcons()):?>
    <div class="product-icons">
        <?php echo $_helper->productAttribute($_product,($_product->getIcons()), 'icons') ?>
    </div>
<?php endif; ?>

请检查它是不是拼写错误,它确实是:

$_product->geticons() 

或应该是

$_product->getIcons()

答案 1 :(得分:0)

类似的东西:

<?php
if($_helper->productAttribute($_product,($_product->geticons()), 'icons'))
{
     echo "<div class=\"product-icons\">";
     echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
     echo "</div>";
}
?>

会更好!

答案 2 :(得分:0)

<?php
  if(!empty($_helper->productAttribute($_product,($_product->geticons()), 'icons')))
    {
      echo "<div class=\"product-icons\">";
      echo $_helper->productAttribute($_product,($_product->geticons()), 'icons');
      echo "</div>";
    }
?>