0

This is my php code.

 $list .="<option value='$A $B'> $A,$B </option>";

I want to put a select option based on data. for example if $A = 1 and $B =2 then the option value get selected.

Normaly I would do it through this code

<?php echo $A == '1' ? 'selected' : ''; ?>

but since the $list is already in php quotes I cant.

mymedia
  • 505
  • 5
  • 25
jeet singh
  • 31
  • 5

2 Answers2

1

Since the accepted answer doesn't actually provide the desired effect, I'll post a correct method.

$list.="<option value='$A $B'".($A==1 && $B==2?' selected':'')."> $A,$B </option>";

This is an inline conditional that checks if both $A equals 1 and $B equals 2. When both are true, the attribute is added to the option, otherwise an empty string is added to the option.

mickmackusa
  • 37,596
  • 11
  • 75
  • 105
0

Option 1:

$list .="<option value='$A $B' ". ($A == '1' ? 'selected : '') ."> $A,$B </option>";

Option 2:

$selected = $A == '1' ? 'selected : '';
$list .="<option value='$A $B' $selected> $A,$B </option>";
Dekel
  • 57,326
  • 8
  • 92
  • 123