Question

I am trying to pass condition for id ? How to achieve the below condition

if ( $vm->check( "pagetype = cart" ) )
{
echo $ showRightcolumn? 'contentarea2' : 'newcontentarea';
}
else
echo $showRightColumn ? 'contentarea2' : 'contentarea';

in this statement

<div id="<?php echo $showRightColumn ? 'contentarea2' : 'contentarea'; ?>">
Was it helpful?

Solution

For any complex processing you could always create a function to return the ID that you want to return, and then echo the result of the function, something like:

<?php
  function getId($vm, $showRightColumn) {
    if ($vm->check("pagetype = cart")) {
      return $showRightcolumn ? 'contentarea2' : 'newcontentarea';
    }
    return $showRightColumn ? 'contentarea2' : 'contentarea';
  }
?>
<div id="<?php echo getId($vm, $showRightColumn); ?>">...</div>

However, for simpler conditionals, it may just be better to use a ternary operator which evaluates other ternary statements (as suggested by other answers).

OTHER TIPS

Try the following :

<div id="<?php echo $vm->check( "pagetype = cart" ) 
                    ? ($showRightColumn ? 'contentarea2' : 'newcontentarea') 
                    : ($showRightColumn ? 'contentarea2' : 'contentarea') ; 
         ?>"
>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top