Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

if image is → on-error fetch image on the base of gender value

I’m trying to fetch an image, and if on-error, get the default image based on the gender value.
pls advice How can I fetch an on-error default image on base of gender value ?

echo"
   
<img  src=".'imagefolder/'.$_row['img']."
   
<?php if (".$_row['gender']." != 'Male'):?>

onerror = this.src='female-default.jpg'

<?php else: ?>

onerror = this.src='male-default.jpg'

<?php endif; ?> 
   
>";
   

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Your quotes mean that none of the PHP inside is being evaluated, and you can’t use if/else blocks inside an echo anyway. You can break it apart

echo "<img  src='imagefolder/'" . $_row['img'] . "' ";

if ( $_row['gender'] != 'Male'):

echo "onerror = this.src='female-default.jpg'";

else:

echo "onerror = this.src='male-default.jpg'";

 endif;
   
echo ">";

or you can use a ternary

echo "<img src='imagefolder/{$_row['img']}'" . ($_row['gender'] != 'Male' ? " onerror = this.src='female-default.jpg'" : " onerror = this.src='male-default.jpg'") . ">";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading