【问题标题】:Don't display if attribute is string(0)如果属性是 string(0) 则不显示
【发布时间】:2011-07-06 10:21:09
【问题描述】:

我正在使用以下代码部分在我们的 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) 的值,我需要在编码中进行哪些更改才能获得预期的效果?

【问题讨论】:

  • “不工作”是什么意思?
  • 你确定没有图标时 productAttribute 返回 null 吗?而不是 false 或 0 之类的东西?
  • @Alexander Varwijk - 当实际上没有任何东西时,不能 100% 确定它会返回什么,有没有简单的方法来检查?当您在页面加载后查看页面时,div 标签中没有任何内容,因此假定它的值为 null
  • @Vince Pettit 尝试添加一个echo var_dump($_helper-&gt;productAttribute($_product,($_product-&gt;geticons()), 'icons')); 它可能只是一个空字符串,以防!== null 无法捕获它。
  • 看起来它正在返回 string(0)

标签: php string magento null


【解决方案1】:

这就是你需要的东西,你不需要两次调用相同的功能来获得空结果。定义变量并检查它是否为空(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()

【讨论】:

    【解决方案2】:

    类似:

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

    会更好!

    【讨论】:

      【解决方案3】:

      试试

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

      【讨论】:

      • 或尝试 isset() 而不是 !empty()
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 2016-12-19
      • 2016-11-07
      • 1970-01-01
      • 2019-10-19
      相关资源
      最近更新 更多