【问题标题】:default image when there isnt any image in database数据库中没有图像时的默认图像
【发布时间】:2025-12-24 07:50:15
【问题描述】:

如果该特定 ID 没有任何图像,我想显示coming-soon.jpg 图像。我使用连接查询从两个表中获取数据。所以数据库中有很多不同id的图像。但如果没有任何特定 id 的图像,那么它的鞋子coming-soon.jpg

下面是我的代码。

 <table width="100%" border="0" id="batch">
<? 
include("includes/paging.php");
$p = new post_pager(16); 
$photo_detail="select a.*,b.* from office_photo a,office_master b where a.album_id=b.album_id group by a.album_id";  
$p->query($photo_detail, "b.album_id asc", init());
$thispage = "Photo";
if ($p->page_total > 0 ) {  
$int_count = $p->get_counter();
$i=1;
while($row= tep_db_fetch_array($p->recresult))
{
$file_id=$row["file_id"];
$file_name=$row["file_name"];
$album_name=$row["album_name"];
$sqft=$row["sqft"];
$location=$row["location"];
$year=$row["year"];
$album_id=$row["album_id"];
if($i==1){ ?>
<tr>
<th>Project Name</th>
<th>Sq.Ft.</th>
<th>Loctaion</th>
<th>Year</th>
<th>Gallery</th>
</tr>
<? } ?>
<tr>
<td><a title="<?=$album_name;?>" href="office-photos.php?album_id=<?=$album_id?>" style="text-decoration:none">
<p style="text-align:center;padding-top:5px;width:140px;text-decoration:none"><?=$album_name;?></p></a></td>
<td><?=$sqft;?></td>
<td><?=$location;?></td>
<td><?=$year;?></td>
<td><a title="<?=$album_name;?>" href="office-photos.php?album_id=<?=$album_id?>" style="text-decoration:none">
<img src="upload/gallery_photo/thumb/<?=$file_name;?>"/></a></td>
</tr>
<? 
if($i==4){ 
$i=1; 
?>
<? }else {
$i = $i + 1;
}
}
}
?>
</table>

【问题讨论】:

    标签: php mysql database image


    【解决方案1】:
    if($file_name == '')
    {
    $file_name="defaultimg.jpg";//use your default image name here
    }
    

    【讨论】:

      【解决方案2】:

      在您的 while 循环中,用这一行替换代码。

      $file_name=(!empty($row["file_name"])?$row["file_name"] : 'default-img.jpg');
      

      【讨论】:

      • 我做了如下改变,但没有醒
      • 这样做var_dump($row["file_name"]) 看看你得到了什么。
      • 我进行了如下更改,但仍然无法正常工作 ' while($row= tep_db_fetch_array($p->recresult)) { $file_id=$row["file_id"]; $file_name=(!empty($row["file_name"])?$row["file_name"] : 'default-img.jpg'); $album_name=$row["album_name"];'
      • 您的文件夹thumb 中有名为default-img.jpg 的图像吗?
      • 做这个 var_dump($row["file_name"]) 看看你得到了什么。
      【解决方案3】:
      if(empty($file_name))
      {$file_name="coming-soon.jpg";}//put any default image here
      

      【讨论】: