没有要显示的图像时显示默认图像

时间:2016-11-21 06:56:34

标签: php

我在显示默认图片时遇到问题。我不知道究竟放在哪里。

$result = $conn->query("SELECT * FROM adoption;");

if($result->num_rows !=NULL){
    while($rows = $result->fetch_assoc()) {
        $AAnimalName = $rows['AAnimalName'];
        $Abreed = $rows['Abreed'];
        $Asex = $rows['Asex'];
        $Acolor = $rows['Acolor'];
        $image = $rows ['image'];
        ?>
        <div class="container-custom1">
            <?php echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>
            <?php echo "<i><h1 class='junction'><a style='cursor:pointer' class='junction'>".$AAnimalName."</a></h1></i>"."<br>".$Asex." /".$Abreed."<br>".$Acolor."<br>"?></div>
        <?php
    }
}

5 个答案:

答案 0 :(得分:1)

while循环中尝试此操作:

$image_location = "admin/function/upload/".$image;
if(file_exists($image_location )) {
    echo '<img src = "'.$image_location .'" width = "248" height="190" class="age1" title>';
}
else {
    echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}

答案 1 :(得分:0)

改变这样的一行:

<?php echo '<img onerror="this.src=\'img/logo.png\'" src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>'?>

img/logo.png将是默认图片

答案 2 :(得分:0)

试试这个:

If(file_exist('your_file_path'))
{
    echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190" class="age1" title>';
}
else
{
    echo '<img src = "admin/function/upload/default_image.jpg'" width = "248" height="190" class="age1" title>';
}

这将检查文件是否存在。如果存在,它将显示给定文件。否则是默认文件。

答案 3 :(得分:0)

  $image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;

答案 4 :(得分:0)

使用三元运算符并设置图像

<?php

  $image = (!empty($rows ['image'])) ? $rows ['image'] : "default.png" ;
 $image_location = "admin/function/upload/".$image;
echo '<img src = "admin/function/upload/'.$image.'" width = "248" height="190"    class="age1" title>';

   ?>