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
  • 572
  • 6
  • 26
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
  • 43,625
  • 12
  • 83
  • 136
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
  • 60,707
  • 10
  • 101
  • 129