PHP:如果指定的图像不存在,如何显示默认图像? [英] PHP: How to display a default Image if the specified one doesn't exists?
问题描述
我正在开展一个小项目,我需要在数据库中显示作者的每个作者的图像。窥视下面的代码:
I am working on a little project and I need to display the author's image for each author in my database. peep the code below:
- 查询 -
$getboth_sql = mysql_query(
"SELECT lyrics.lyrics_id, lyrics.lyrics_title, lyrics.lyrics_text,artists.artist_nane,artists.artist_id
FROM lyrics,artists
WHERE lyrics.artist_id = artists.artist_id
ORDER BY lyrics.lyrics_title");
while ($both = mysql_fetch_assoc($getboth_sql)) {
$lyrics_id = $both[lyrics_id];
$lyrics_title = $both[lyrics_title];
$lyrics_text = $both[lyrics_text];
$artist_name = $both[artist_nane];
$artist_id = $both[artist_id];
?>
<div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg" height="50px" width="50px"/></div>
<?php
}//END While
上述代码可以正常工作,但如果我没有在'artists'目录中保存图像,则不会显示该艺术家的图像。
The above code work but if I have not saved an image in the 'artists' directory no image show up for that artist.
<div class="artimg"><img src="images/artists/<?php echo $artist_name;?>.jpg" height="50px" width="50px"/></div>
问题:如果找不到指定的on,显示默认图像的最佳方法是什么。
QUESTION: What is the BEST way to display a default image if the specified on is not found.
我一直在玩空文件和存在的函数,但我没有把它弄好。请帮忙。
I have been playing around with the empty and file exists functions but i haven't gotten it right. Please Help.
PS如果这个问题看起来很愚蠢,我不是专业人士!
PS I'm not a pro if this question seems stupid!
推荐答案
有很多方法可以做到这一点。
There are many ways to do this.
<?php
if(file_exists("images/artists/$artist_name.jpg"))
$fileName = "$artist_name.jpg";
else
$fileName = "default.jpg";
?>
<div class="artimg"><img src="images/artists/<?php echo $fileName;?>" height="50px" width="50px"/></div>
这篇关于PHP:如果指定的图像不存在,如何显示默认图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!