This question already has an answer here:
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.
</div>
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>";
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.