带变量php的动态活动类

时间:2016-09-09 07:23:44

标签: php html

我想使用php将class= "active"添加到我的链接中,但我遇到的挑战是我的链接附加了变量.php?foobar=somevariable。所以,有些人如何忽略班级的活跃程度。这是我的代码段

<?php
//we need to set a var to find what page we are on
$pageOn2 = basename($_SERVER['PHP_SELF']);

?>

以下是我的HTML

<li class="<?php if($pageOn2 == 'regionalsummits.php?summit=eastern'){?> current-menu-item <?php }?>">
                                <a href="regionalsummits.php?summit=eastern">Eastern Region</a>
                            </li>
                             <li class="<?php if($pageOn2 == 'regionalsummits.php?summit=southern'){?> current-menu-item<?php }?>">
                                <a href="regionalsummits.php?summit=southern">South Western Region</a>
                            </li>
                            <li class="<?php if($pageOn2 == 'regionalsummits.php?summit=western'){?> current-menu-item<?php }?>">
                                <a href="regionalsummits.php?summit=western">Western Region</a>
                            </li>

任何关于哪里出错的帮助

3 个答案:

答案 0 :(得分:0)

尝试在变量中保存参数,然后检查

<?php
$variable = $_GET['foobar'];
?>

<li class="<?php if($variable == 'eastern'){ echo 'current-menu-item'; } ?>"> <a href="regionalsummits.php?summit=eastern">Eastern Region</a> </li>
<li class="<?php if($variable == 'southern'){ echo 'current-menu-item'; }?>"> <a href="regionalsummits.php?summit=southern">South Western Region</a> </li>
<li class="<?php if($variable == 'western'){ echo 'current-menu-item'; } ?>"> <a href="regionalsummits.php?summit=western">Western Region</a> </li>

答案 1 :(得分:0)

$pageOn2 = basename($_SERVER['PHP_SELF']);在这行代码中,变量$pageOn2将接收值regionalsummits.php它将不会获取查询字符串cuz basename($_SERVER['PHP_SELF']);这将不会返回查询字符串

所以你可以这样做:

$pageOn2 = basename($_SERVER['PHP_SELF'])."?".$_SERVER['QUERY_STRING'];

答案 2 :(得分:0)

尝试这种方式,您可以通过其他方式实现。 我添加了一个片段以便更好地理解。

<?php
$pageOn2 = $_SERVER["QUERY_STRING"];
?>
<li class="<?php if ($pageOn2 == 'summit=eastern') { ?> current-menu-item <?php } ?>">
    <a href="regionalsummits.php?summit=eastern">Eastern Region</a>
</li>
<li class="<?php if ($pageOn2 == 'summit=southern') { ?> current-menu-item<?php } ?>">
    <a href="regionalsummits.php?summit=southern">South Western Region</a>
</li>
<li class="<?php if ($pageOn2 == 'summit=western') { ?> current-menu-item<?php } ?>">
    <a href="regionalsummits.php?summit=western">Western Region</a>
</li>

<style>
    .current-menu-item
    {
        background-color:red;  
    }
</style>