Load alternative image in case you don’t have the correct image

Asked

Viewed 182 times

0

I have this code that loads the image of the player according to its name, it is possible to complement this code by loading a default photo, when the code does not find the photo of the player?

$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";

2 answers

2

Answer:

$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";
if (file_exists($player_img)){
    echo '<img src="'.$player_img.'"></img>';
} else {
    echo '<img src="players/default.png"></img>';
}
  • 1

    Thank you so much for helping, the code worked but the photo is loading at the top of the page and not at the place where it should be loaded.

  • I recommend creating another question in the OS for this.

2


To run the code normally just reset "$player_img" if the reference does not point to an image on the server.

// receber a referencia noralmente
$player_img = "players/" . $row['LastName'] . "_" . $row['FirstName'] . ".png";
// verificar se não existe
if( !file_exists($player_img) ){
   // definir uma imagem padrão
   $player_img = "players/default.png";
}
  • It worked perfectly, thank you!

Browser other questions tagged

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