我想在回声中放置一个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') {
}
在该声明中包装图像的最佳方法是什么?谢谢!
答案 0 :(得分:1)
您实际上可以结束控制流程块,例如在打开它们的同一个PHP块之外的if语句。例如,这应该有效:
<?php if (!$hideProduct) { ?>
<div class="gearBorder">
<?php if ($productStatus == '0') { ?>
<img title="Sold Out" ... />
<?php } ?>
...HTML...
</div>
<?php } ?>
如果你不喜欢花括号,你也可以分别用冒号(:)和endif替换它们。有关详细信息,请参阅this link。
答案 1 :(得分:0)
使用数组来保存每个$ productStatus的CSS类(带有background-image)
快速高效。当您从HTML模式切换到PHP模式时,性能会很高。此方法消除了英特尔微代码中的if elseif性能损失。
<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...');