kenny kenny - 6 months ago 15
PHP Question

hide <img> field if no data in database

So I'm pulling data from my database that users enter in. I want to avoid a blank img box if they have not submitted an image.

For example, if the user doesn't have an $image2 in his/her database, avoid displaying the class for that specific image. Is this even possible?

any help would be greatly appreciated!

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" >
<?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?>
</a>

<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" >
<?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?>
</a>

Answer

use simple if{} condition like below:

<?php if(!empty($userRow['image2'])) { ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image2'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image2']."' alt='Profile Pic'>";?></a>   
<?php } ?>

<?php if(!empty($userRow['image3'])) { ?>
<a class="example-image-link" href="<?php echo 'pictures'.'/'.$userRow['image3'];?>" data-lightbox="example-set" ><?php echo "<img class='example-image' src='pictures/".$userRow['image3']."' alt='Profile Pic'>";?></a>
<?php } ?>
Comments