Powerballs Powerballs - 11 days ago 5
PHP Question

Displaying default image when there is no image to display

I am having problem with displaying default image. I do not know where exactly to place it.

$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
}
}

Answer

try this inside while loop :

$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>';
}
Comments