0

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.

4

3 回答 3

2
<td><?php echo (!empty($rows['photo1']) ? '<img src="Photos/' . $rows['photo1'] . '" height="200" />' : '') ?></td>
于 2012-08-12T22:27:52.180 回答
1

要让空白值不<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>

希望这可以帮助 :)

于 2012-08-12T22:59:54.600 回答
-1

也许这更符合您的喜好:

 <img src="Photos/<? echo $rows['photo1']; ?>" height="200" onerror="this.style.display='none'" />

它将图像设置为在出现问题(例如 URL 损坏)时不显示。

这样做的好处是避免了三元组,将 PHP 与 HTML 分开,并且无需过多的文档就可以保持清晰。希望能帮助到你。

于 2012-08-12T22:39:50.010 回答