Display default image, if it does not exist in the database with php

Viewed 239

I made a condition that when there is no image in my database, I return a standard image, however, I am having a small problem in my syntax and I have not been able to find a solution so far.

The problem is that I am not able to pass my $user['photo'] variable inside my echo

This is my code

if(!empty($imagem_de_perfil) == null) {
   echo '<img src="../../painel/public/images/no_image.png" alt="Imagem de perfil" width="160" height="120">';
} else {
   echo '<div style="background: url(<?= base_url() ?><?= $user['photo'] ?>); position: relative; left: 48%; background-size: 48px 48px; width: 48px; height: 48px;" class="upload_photo upload_photo_user">
  </div>';
}

enter image description here

2 Answers

You could store default image path in database as default value in your image row, then query databse to get image path and with html and php show that image

<img src = "<?php echo $user['image']; ?>">
if(!empty($imagem_de_perfil) == null) {
   echo '<img src="../../painel/public/images/no_image.png" alt="Imagem de perfil" width="160" height="120">';
} else {
   $userImage = $user['photo'];
   echo "<div style='background: url(<?= base_url() ?><?= $userImage ?>); position: relative; left: 48%; background-size: 48px 48px; width: 48px; height: 48px;' class='upload_photo upload_photo_user'>
  </div>";
}
Related