-3

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; ?> 
   
>";
   
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
ravi
  • 1
  • 4
  • You can't have if/else blocks in the middle of an echo. Look into using a ternary instead – aynber Dec 30 '22 at 16:10
  • already added→ – ravi Dec 30 '22 at 16:15
  • 1
    That's still part of an if/else, which **you cannot have inside of an echo**. here is information on [ternary operators](https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary). Though with your quotes, the PHP inside is actually being sent right to the browser instead of evaluated – aynber Dec 30 '22 at 16:16
  • i am a learner can you pls advice the correct way to fetch image – ravi Dec 30 '22 at 16:20

1 Answers1

1

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'") . ">";
aynber
  • 22,380
  • 8
  • 50
  • 63
  • oh really thanks its working fine ,,,, thanks a lot !! i used this (or you can use a ternary ) thanks a lot ! – ravi Dec 30 '22 at 16:38