Hello I have a table that stores user profile information as well as their profile picture.
I am currently able to echo the users avatar but my question is how can I echo a default.png avatar if the column is left null. in other words they havent uploaded a profile picture.
<div id="imagePreview" style="background-
image: url(avatars/<?php echo
$avatar['avatar']; ?>);">
</div>
CodePudding user response:
try this
<div id="imagePreview" style="background-
image: url(avatars/<?php if($avatar['avatar']==""){echo
'imagedefault.png';}else{echo $avatar['avatar'];}?>);">
</div>
CodePudding user response:
Will you use the avatar more than one in your script? if so, you can create a variable that stores the value or call the default one. Something like that:
<?php
$avatar = !empty($avatar['avatar']) ? $avatar['avatar'] : 'default.jpg';
?>
<div id="imagePreview" style="background-image: url(avatars/<?=$avatar;?>);"></div>
If you are using it once, you can do it without a variable:
<div id="imagePreview" style="background-image: url(avatars/<?=!empty($avatar['avatar']) ? $avatar['avatar'] : 'default.jpg';?>);"></div>