PHP - if null

I have the following encoding:

<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> 

I would like to add an if statement, mainly to say the following:

If the value in the attribute 'energy_rating_one' is null, then do not display the division energy-rating-1 , if the attribute 'energy_rating_two' is zero, then do not display the div energy-rating-2 and so on ...

+4
source share
5 answers

something like that:

 <?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; ?> 

as well as for everyone else.

+4
source
  <?php function echoIfExists($argument) { $val = $_product->getAttributeText($argument); if($val) /*your echo stmt*/ } echoIfExists('energy_rating_one'); /** continue here*/ ?> 
0
source

Calm yourself. Just change the CSS class rules from energy-rating-1 to energy-rating-one and repeat the variable that you already have ie energy-rating-one

0
source

You have an int_to_words function to convert the int value to a text value in this post: http://www.php.net/manual/en/function.strval.php#41988

After that, you just need to iterate through all the values

 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>"; } } 
0
source

Take a look at the short if statement:

http://www.scottklarr.com/topic/76/ternary-php-short-hand-ifelse-statement/

 $total==1 ? "is 1 item" : "are ".($total == 0 ? "no items" : "$total items" 
-3
source

All Articles