Powerballs Powerballs - 18 days ago 9
PHP Question

Displaying default image when there is no image to display PHP

> <?php include ('admin/connection.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
>
> } }
>
> ?>


Hi this is my working code. Having problem on my displaying default image php code. I don't know where exactly to place it. Can somebody help me determine?

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