【问题标题】:How to make a condition true in a class?如何在课堂上使条件为真?
【发布时间】:2017-03-03 10:45:49
【问题描述】:
<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); // get current url 
<?php $url = $category->getURL(); ?> 
<li class="<?php if ($currentUrl == $url) {echo 'active';} else{echo '';} ?> dropdown megamenu-fw "></li>

如果当前 url 等于当前 li url,我正在尝试在类的 li 标记中创建一个条件,然后添加活动的类,但它不起作用。

【问题讨论】:

  • 但它不起作用。这是什么意思?你看到了什么错误?
  • 以上条件不起作用,这是我的意思
  • 这段代码没有添加类
  • 首先,第一行缺少一个结束标记?>。其次,你应该让你的前两行看起来像:&lt;?php $currentUrl = Mage::helper('core/url')-&gt;getCurrentUrl(); // get current url $url = $category-&gt;getURL(); ?&gt;。第三,我会做一个var_dump($currentUrl.'--------'.$url);die; 来检查变量中的内容。

标签: php html mage


【解决方案1】:

你可以试试这个。

如果下面的代码不起作用,那么您可以检查$currentUrl$url 的第一个变量值。

<?php $currentUrl = Mage::helper('core/url')->getCurrentUrl(); // get current url 
<?php $url = $category->getURL(); ?>
<?php if ($currentUrl == $url) {echo '<li class="active dropdown megamenu-fw "></li>';} else { echo '<li class="dropdown megamenu-fw"></li>';} ?> 

【讨论】:

    【解决方案2】:

    网址对吗? if 语句可以是真的吗? 您可以尝试以其他方式获取 URL:

    $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    

    顺便说一下(可能不是真正的问题):
    您的第 1 行将发送错误:您在第 1 行打开 php &lt;?php 但没有关闭它,而不是在第 2 行您再次打开 php 第 2 行:&lt;?php ... ?&gt;

    【讨论】:

    • 是的,网址是正确的,我确实回显了打印正确网址的内容
    • 是的,对不起,我忘了在这里关闭 php,但我在我的文件中关闭了
    • 所以我认为没有问题。您试图显示所有错误? error_reporting(-1);
    猜你喜欢
    • 1970-01-01
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2019-05-13
    相关资源
    最近更新 更多