2014-07-08 49 views
0

我想在回聲中放置if語句,我不太確定如何去做。這裏是回聲:如何在echo中添加PHP if語句?

if(!$hideProduct) {   
echo ' 
    <div class="gearBorder"> 
     <img title="Sold Out" alt="Sold Out" class="soldOut" src="soldout.png" />':"").' 
     <div class="gearInfo"> 
      <h4>' . $productName . '</h4> 
      <p class="gearDesc">'. $productDescription .'</p> 
      <p class="cost">$' . $productPrice . '</div> 
     </div> 
    </div> 
';} 

在第3行,我想換圖像中的if語句:

if($productStatus = '0') { 

} 

什麼是包裹在聲明中形象的最佳方式是什麼?謝謝!

回答

1

實際上,你可以結束控制的流程框一樣,如果他們被打開同一PHP塊外的語句。例如,這應該工作:

<?php if (!$hideProduct) { ?> 
    <div class="gearBorder"> 

     <?php if ($productStatus == '0') { ?> 
      <img title="Sold Out" ... /> 
     <?php } ?> 

     ...HTML... 

    </div> 
<?php } ?> 

如果你不喜歡大括號,你也可以分別用冒號(:)和endif替換它們。有關更多信息,請參閱this link

+0

完美,謝謝你的例子和資源! – user13286

0

使用數組來保存每個$ productStatus的CSS類(帶背景圖像)
快速高效。從HTML模式切換到PHP模式時,性能很熱。這種方法消除了英特爾微代碼中ifif性能的下降。

<style type="text/css"> 
.soldout{width:40px;height:40px;background-image: url('soldout.png');} 
.backorder{width:40px;height:40px;background-image: url('backorder.png');} 
.instock{width:40px;height:40px;background-image: url('instock.png');} 
</style> 

$icon = array(' class="soldout" ',' class="backorder" ',' class="instock" ');. 

echo '<div class="gearBorder"><div ' . $icon[$productStatus] . '></div><div class="gearInfo"> ... '; 

我也將使用4位色GIF圖標並將其轉換爲Base64 MIME
確保頁供應用gzip將會有很少或幾乎沒有懲罰的Base64編碼。
如果您想堅持使用圖像文件,請確保圖像以最大緩存最大值進行投放。

background-image: url('data:image/gif;base64,R0lGODlhKAAoAK...');