Richard Young Richard Young - 1 year ago 90
PHP Question

Ternary toggle in php

I'm trying to set the order of something using options from a dropdown. I'm putting the order into a variable and I'm trying to use that variable to decide which option to have as selected but for some reason it won't work. Can anyone work out what I'm doing wrong please?

<option value="DESC" <? ($comment_order == "DESC" ? "selected" : "");?>>Descending</option>
<option value="ASC" <? ($comment_order == "ASC" ? "selected" : "");?>>Ascending</option>

Answer Source

Your statement does not output anything. You want to put echo before it:

 <? echo ($comment_order == "DESC" ? "selected" : "");?>