我有以下编码:
<div class="product-top-icons">
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<div class="energy-rating-2"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_two');?>.jpg"></div>
<div class="energy-rating-3"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_three');?>.jpg"></div>
<div class="guarantee-icon"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('warranty');?>.jpg"></div>
</div>
我想在那里添加一个if语句,基本上说如下:
如果'energy_rating_one'
属性中的值为空,则不显示分区energy-rating-1
,如果'energy_rating_two'
属性为空,那么不显示div energy-rating-2
,依此类推…
类似这样的东西:
<?php if($_product->getAttributeText('energy_rating_one') !== null): ?>
<div class="energy-rating-1"><img src="http://www.justhome.co/skin/frontend/default/just2011/images/assets/<?php echo $_product->getAttributeText('energy_rating_one');?>.jpg"></div>
<?php endif; ?>
其他人也是如此。
<?php
function echoIfExists($argument) {
$val = $_product->getAttributeText($argument);
if($val)
/*your echo stmt*/
}
echoIfExists('energy_rating_one');
/** continue here*/
?>
让自己轻松一点。只需将css类规则从energy-rating-1
更改为energy-rating-one
,并回显已有的变量,即energy-rating-one
在这篇文章中,您有一个函数"int_to_words"可以将"int"值转换为文本值:http://www.php.net/manual/en/function.strval.php#41988
之后,您只需要迭代到的所有值
for(i = 0; i < 100; i++) {
$item = 'energy_rating_'.int_to_words($i);
if($_product->getAttributeText($item) != null){
echo "<div class='"energy_rating_$i'"><img src='"http://www.justhome.co/skin/frontend/default/just2011/images/assets/".$_product->getAttributeText($item).".jpg'"></div>";
}
}
查看短手if语句:
http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/
$total==1 ? "is 1 item" :
"are ".($total == 0 ? "no items" : "$total items"