Display default image if not in the database

Asked

Viewed 41 times

0

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

The problem is I’m not getting past my variable $user['photo']; inside my echo

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

Erro que aparece ao concatenar

  • The problem is the ' that you start the string and are breaking in the middle of it.

2 answers

0

The error is in that line

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

Have you stated <?php up there, you’d only use <?= $user['photo'] ?> if you were in the middle of HTML outside of PHP.

The correct is to concatenate variable with the string, thus:

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

Another point, if you want to use a variable within a string, you should use double quotes, when you use single quotes, you need to concatenate.

echo " Essa é minha variavel $variavel "; //FUNCIONA

echo ' Essa é minha variavel $variavel '; // NÃO FUNCIONA

echo ' Essa é minha variavel '. $variavel; //FUNCIONA

0

I think that would solve it. The problem is that you need to do the escape quotes and also concatenate the string with the function you called and with the $user["photo"]

if (!empty($imagem_de_perfil)) {
  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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.