Erick Asto Oblitas
Erick Asto Oblitas

Reputation: 1409

Conditional Statements(php) between html attributes?

I'm refactoring a php web page and I need to insert a class dynamically.

By structure of the project, one solution is put a ternary operator <?($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?> to handle the value of a variable assigned before:

<a id="customid_generatebytool" class="<?($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?>" href="anotherPage.php" ...>
....
</a>

<a id="customid_generatebytool" class="<?($selectedElement=="KeyTwo"? 'linkactive': 'normalLink') ?>" href="Page2.php" ...>
....
</a>

But my code above doesn't work, I'm new in php.

Thanks in advance.

Upvotes: 1

Views: 1836

Answers (2)

Irek Kubicki
Irek Kubicki

Reputation: 57

You have used php short tags <?. Make sure it's enabled in your php.ini.

Upvotes: 0

emsoff
emsoff

Reputation: 1605

Add echo.

<a id="customid_generatebytool" class="<?php echo ($selectedElement=="KeyOne"? 'linkactive': 'normalLink') ?>" href="anotherPage.php" ...>

Upvotes: 3

Related Questions