I have the following line:
<td><img src="Photos/<? echo $rows['photo1']; ?>" height="200" /></td>
I don't always have a photo. I would like to hide the image space. It looks like it is broken or if the url is wrong.
<td><?php echo (!empty($rows['photo1']) ? '<img src="Photos/' . $rows['photo1'] . '" height="200" />' : '') ?></td>
要让空白值不<img>
显示标签,我会使用如下内容:
<?php if (($rows['photo1'] !== "") || ($row['photo1']))
{
echo "<td><img src='Photos/" . $rows['photo1'] . "' height='200' /></td>";
}
?>
if (image is not blank, and it exists) { then, echo the img tag and the variables }
或者,如果您想为空白值显示不同的图像:
<td>
<img src="Photos/<?php if ($rows['photo1'] !== ""){echo $rows['photo1'];}
else {echo "defaultimg.jpg";}?>" height="200" />
</td>
希望这可以帮助 :)
也许这更符合您的喜好:
<img src="Photos/<? echo $rows['photo1']; ?>" height="200" onerror="this.style.display='none'" />
它将图像设置为在出现问题(例如 URL 损坏)时不显示。
这样做的好处是避免了三元组,将 PHP 与 HTML 分开,并且无需过多的文档就可以保持清晰。希望能帮助到你。