简体   繁体   English

PHP生成的导航将一个类分配给活动页面

[英]PHP Generated Navigation assign a class to active page

I'm trying to assign a class='active' to the page your viewing on the moment. 我正在尝试将class='active'分配给您当前查看的页面。 My menu is generated with PHP from an array, see the code bellow: 我的菜单是用PHP从数组生成的,请参见下面的代码:

<?php
    $navmenu = array(
        'index' => 'Home',
        'about' => 'About',
        'page3' => 'Page3',
        'page4' => 'Page4',
        'page5' => 'Page5',
        'page6' => 'Page6'
    );
?>

<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
    <ul class="nav navbar-nav navbar-right">
        <?php
            foreach ($navmenu as $url => $diplay) {
                echo "<li><a href='{$url}.php' class=''>{$diplay}</a></li>\n";
            }
        ?>
    </ul>
</div>

I would like to add class='active' to the page you'r viewing at the moment, but I have no idea how to do that, in simple HTML will be like this: 我想在当前正在查看的页面上添加class ='active',但我不知道如何执行此操作,简单的HTML格式如下:

<li class="active"><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="page3.html">Page3</a></li>
<li><a href="page3.html">Page4</a></li>
<li><a href="page3.html">Page5</a></li>
<li><a href="page3.html">Page6</a></li>

Let me know, what options I have to implement this. 让我知道,我必须执行哪些选择。 Thanks! 谢谢!

One Option would be to read the URL and compare it in the foreach loop. 一种选择是读取URL并在foreach循环中进行比较。

$_SERVER['REQUEST_URI']  

That will get you the URL portion you need. 这将为您提供所需的URL部分。

Then you just compare that in your foreach loop. 然后,只需在foreach循环中进行比较即可。

foreach ($navmenu as $url => $diplay) {
    $addClass = "";
    $urlArray = explode("/", $_SERVER['REQUEST_URI']);
    $urlArraySize = sizeof($urlArray);

    if($urlArray[$urlArraySize - 1] == $url."php")
    {
        $addClass = ' class="active"';
    }

    echo "<li".$addClass."><a href='{$url}.php' class=''>{$diplay}</a></li>\n";
}

NOTE: This has a couple more steps than needed, but it does an exact comparison to the last value (which is your page value) in the URL. 注意:这比需要的步骤多了几个,但是它与URL中的最后一个值(这是您的页面值)进行了精确比较。 This will not account for any parameters passed via the URL. 这将不考虑通过URL传递的任何参数。

Just compare if your current URL (stored in $_SERVER['REQUEST_URI'] is part of the url you are about to print and in this case add class="active" to your output. 请比较一下当前的URL(存储在$ _SERVER ['REQUEST_URI']中)是否是要打印的URL的一部分,在这种情况下,将class =“ active”添加到输出中。

Remember that this solution can have trouble if you have urls that contain the same name parts such as "index" and "profile_index_page" would generate a false positive. 请记住,如果您的网址包含相同名称的部分(例如“ index”和“ profile_index_page”)会产生误报,则此解决方案可能会遇到麻烦。

For a 100% solution to this problem you need to use alot of regex or create a little router with predefined routes. 对于此问题的100%解决方案,您需要使用大量正则表达式或创建带有预定义路由的小型路由器。

Also this solution is very static as you lack the ability to add any custom html attributes for later purposes but I dont want to go too far here, you will know if this solution fits your requirements. 另外,此解决方案非常静态,因为您缺乏为以后的目的添加任何自定义html属性的能力,但是我不想在这里做太多,您将知道此解决方案是否符合您的要求。

$output = "";
foreach ($navmenu as $url => $diplay) {
    $output .= '<li><a ';
    if(strpos($_SERVER['REQUEST_URI'], $url) !== false) {
        $output .= 'class="active" ';
    } 
    $output .= '>' .$diplay . '</a></li>\n';
}
echo $output;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM