Difficulty deploying multiple options in a conditional variable to load corresponding image

Asked

Viewed 30 times

0

like every programmer... it’s 1:30 in the morning.... That’s us....

Friends can’t make a conditional variable work... someone can give me a light....?

<?php


	$variavel_condicional = $data["CreditData"][0]["Score"]["ScoreType"] == 'RESTRICAO' ? '<img src="api/images/com_restricao.png">' : '<img src="api/images/sem_restricao.png">'; // OK FUNCIONA
	
	// ["Class"] VAI RETORNAR UMA LETRA, PODE SER: A, B, C, D OU E e deacordo com a letra que ela retorna preciso que carregue a imagem corresponde a.png ou b.png ou ...
	// poem não estou conseguindo fazer isso... algume pode me ajudar?
	$variavel_condicional_letra = $data["CreditData"][0]["Score"]["Class"] == 'A' ? '<img src="api/images/a.png">'  : == 'B' ? '<img src="api/images/b.png">'  :== 'C' ? '<img src="api/images/c.png">'  :== 'D' ? '<img src="api/images/d.png">'  : '<img src="api/images/e.png">';
	
	
	echo "<tr>";
	
	echo '<td>' .$variavel_condicional.'</td>';  // OK FUNCIONA
	
	
	echo '<td>' .$variavel_condicional_letra.'</td>'; 

	echo "</tr>";
	
?>

Thanks friends...

1 answer

0


I believe it is not appropriate, but it must solve:

$variavel_condicional_letra = ($data["CreditData"][0]["Score"]["Class"] == 'A') ? '<img src="api/images/a.png">'  :($data["CreditData"][0]["Score"]["Class"] == 'B') ? '<img src="api/images/b.png">'  : ($data["CreditData"][0]["Score"]["Class"] == 'C') ? '<img src="api/images/c.png">'  : ($data["CreditData"][0]["Score"]["Class"] == 'D') ? '<img src="api/images/d.png">'  : '<img src="api/images/e.png">';

Edit: So do it using a array with the values, I believe it is the most appropriate:

$imagem=$data["CreditData"][0]["Score"]["Class"];
$arr=array('A'=>'<img src="api/images/a.png">','B'=>'<img src="api/images/b.png">','C'=>'<img src="api/images/c.png">','D'=>'<img src="api/images/d.png">','E'=>'<img src="api/images/e.png">');

echo $arr[$imagem];
  • Wees Smith... thanks for the feedback.... but it didn’t work out... In fact it almost worked... the problem is that it always returns the option D, even if the Class is A or B or C always return me the image d.png

  • I edited with a possibility.

  • 1

    Wees Smith, ball show! Thanks now worked perfectly.... thanks friend!

Browser other questions tagged

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