Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

The following PHP in a node tpl file:

check_plain($node->field_room[$node->language][0]['value'])

Will print this in the page: Value1

What I want however is if the field has a value of 'Value1' then I want to print something else on the page. Ive tried the following but I think I have a syntax error:

if ( check_plain($node->field_room[$node->language][0]['value']) == "Value1" ) {
                        echo "<p>Print something else </p>";
                    }
share|improve this question
You should not need the check_plain() call for this purpose, by the way. I don't think it's the cause of the problem, though, unless the value you are actually checking for has HTML special characters in it. – Alfred Armstrong Mar 19 at 17:01

1 Answer

up vote 2 down vote accepted

There's no syntax error in the second code sample, if you're getting a syntax error it's not coming from those lines.

The best way to get values from an entity is with field_get_items():

$items = field_get_items('node', $node, 'field_room');
if (count($items) && check_plain($items[0]['value']) == 'Value1') {
  // Do something
}

But unless you have some sort of multi-lingual issue the code you've got should do exactly the same thing as that, so the problem might be elsewhere.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.