【问题标题】:How to set condition for div id如何设置 div id 的条件
【发布时间】:2014-04-29 20:15:23
【问题描述】:

我正在尝试传递 id 的条件?如何达到以下条件

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

在此声明中

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

【问题讨论】:

标签: php html css wordpress joomla2.5


【解决方案1】:

对于任何复杂的处理,您总是可以创建一个函数来返回您想要返回的 ID,然后回显该函数的结果,例如:

<?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>

但是,对于更简单的条件,最好使用评估其他三元语句的三元运算符(如其他答案所建议的那样)。

【讨论】:

    【解决方案2】:

    尝试以下方法:

    <div id="<?php echo $vm->check( "pagetype = cart" ) 
                        ? ($showRightColumn ? 'contentarea2' : 'newcontentarea') 
                        : ($showRightColumn ? 'contentarea2' : 'contentarea') ; 
             ?>"
    >
    

    【讨论】:

    • @Waqas Khan:已更新。您的代码中有双重理解:$showRightColumn$showRightcolumn 是如何拼写的?
    猜你喜欢
    • 2015-12-20
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多