0

I'm trying to put this string that work fine as php:

if ($sortvalue == $sort[3]) { echo 'selected=' ;}

into this code:

echo '<select onchange="this.form.submit();" name="sort">
<option value="'.$sort[3].'" HERE_THE_PHP_CODE>Price</option>
[..]
</select>';

I tried a lot of syntax but I got always a blank page.

2

2 Answers 2

2
$selected = '';

if ($sortvalue == $sort[3]) { $selected = 'selected'; }

echo '<select onchange="this.form.submit();" name="sort">
<option value="'.$sort[3].'" $selected>Price</option>
</select>';

This should do what you want. (the reply by Osama has an error in the if statement)

Sign up to request clarification or add additional context in comments.

Comments

1

To keep the code clean you should use the ternary operator. It will allow you to do an if statement inside your echo.

echo '<select onchange="this.form.submit();" name="sort">
<option value="'.$sort[3].'" ' . ($sort[3] == $sortvalue ? 'selected' : '') . '>Price</option>
[..]
</select>';

2 Comments

Thanks! That worked! How is it possible just not to insert the "if" condition and make it works anyway??? I would never find this solution by myself..
@Rain_xx you are using the if condition by using the ternary operator, for more information, please check the link provided by Marwelln

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.