0

i know this is really obvious but i just don't know what I'm missing.

$option[] = "<option value='user' selected=' " . ($row[5]=='admin') ? echo 'selected' : null. "'>user </option>";`

thanks

1
  • 1
    What's the problem? ' don't have to be escaped in a double quote string. Commented Jun 2, 2011 at 13:21

3 Answers 3

2

Try this:

$option[] = '<option value="user" . ($row[5]=='admin' ? 'selected' : '') . '>user </option>';`

Three differences from your code:

  1. I reversed your use of quotes...easier to wrap a PHP string that contains HTML in single quotes (since HTML uses double quotes).
  2. I modified the parenthesis around your ternary operator...it was only wrapped around the condition, and not the true/false return values.
  3. In your "true" return value in the ternary operation, you were calling echo, which isn't necessary in a concatenation operation.
Sign up to request clarification or add additional context in comments.

8 Comments

awesome it worked, however why does yours work not mine?? please explain
@dgamma3: Because you cannot use echo there.
HTML uses "Whatever quotes you like", they can be double quotes, but single quotes are fine. (You can use no quotes at all if the attribute value consists entirely of letters (a-z and A-Z), digits (0-9), hyphens (ASCII decimal 45), periods (ASCII decimal 46), underscores (ASCII decimal 95), and colons (ASCII decimal 58). )
Good comment...this is true about HTML and XHTML.
@AJ actually i found another error. when i use your code i get this in firefox. <option ""="" value="user">user</option> <option "="" "selected="selected" value="user">admin</option> where are the "="" coming from???
|
2

You do not need to write

value=\'user\'

Just do

value='user'

Escaping is needed when you use the same quote marks like:

echo 'Mark\'s dinner';

Comments

0

For starters, I'd break it up. Also, swap your double quotes for single quotes:

$option[] = implode('', array(
    '<option value="user" selected="',
    $row[5] == 'admin' ? 'selected' : '',
    '">user</option>'
));

Comments

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.