用php显示当前页面的活动导航 [英] Show active navigation for current page with php

查看:90
本文介绍了用php显示当前页面的活动导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 $ current_url = basename($ _ SERVER ['PHP_SELF']); 来确定我在哪个页面上,考虑到我的导航(html )存储在一个php文件中,并包含在每个页面中。这是我用来确定哪个导航选项应该激活的代码:

 < link rel =stylesheethref = CSS / style.css中> 
< link rel =stylesheethref =css / no-background.css>

< ul>
<?php
$ current_url = basename($ _ SERVER ['PHP_SELF']);
$ active =class = \active \;
?>
<?php if($ current_url ==globaluser.php){?>
< li<?php echo $ active;?> > <?php}?> < a href =globaluser.php?Agent =<?php echo $ Agent;?>>概述< / a>< / li>
<?php if($ current_url ==search.php){?>
< li<?php echo $ active;?> > <?php}?> < a href =search.php?Agent =<?php echo $ Agent?>>添加新客户端< / a>< / li>
<?php if($ current_url ==viewadmins.php){?>
< li<?php echo $ active;?> > <?php}?> < a href =viewadmins.php?Agent =<?php echo $ Agent?>>查看管理员< / a>< / li>
< li class =border-right>< a href =emails.php?Agent =<?php echo $ Agent?>>电子邮件< / a>< /立GT;
< li class =right border-right>< a href =logout.php><?php echo $ Agent?>< / a>
< ul class =drop1>
< li>< a href =earnings.php?Agent =<?php echo $代理?>>收益< / a>< / li>
< li id =hover-trigger>< a href =#>更改密码< / a>
< ul class =drop2>
< li>
< form action =changepass.php?Agent =<?php echo $ Agent?>方法= POST >
< input type =passwordname =1placeholder =输入新密码>
< input type =passwordname =2placeholder =重复新密码>
< / li>
< li>
< input type =submitname =changepwclass =buttonvalue =Change>
< / form>
< / li>
< / ul>
< / li>
< li>
< form action =logout.php>
< input type =submitclass =buttonvalue =注销>
< / form>
< / li>
< / ul>
< / li>

但它不起作用。它将活动页面显示为活动页面,但是,它将2页面显示为简单的超链接。这就是它的样子:





有人对此有何线索?它适用于2,但2失败,完全相同的代码?
谢谢

解决方案

您应该这样做:

 < li<?php echo($ current_url ==globaluser.php)? $ active:''?> >< a href =globaluser.php?Agent =<?php echo $ Agent;?>>概述< / a>< / li> 

因为现在您已经调整了开头< li> ; 标记


I'm trying to use use $current_url = basename($_SERVER['PHP_SELF']); to determine which page I am on, considering my navigation (html) is stored in a php file and is included in every page. This is the code I use to determine which navigation option should be active:

<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/no-background.css">

<ul>
<?php
    $current_url = basename($_SERVER['PHP_SELF']);
    $active = "class=\"active\"";
?>
<?php if ($current_url == "globaluser.php") { ?>
    <li <?php echo $active;?> > <?php } ?> <a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li>
<?php if ($current_url == "search.php") { ?>
    <li <?php echo $active;?> > <?php } ?> <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li>
<?php if ($current_url == "viewadmins.php") { ?>
    <li <?php echo $active;?> > <?php } ?> <a href="viewadmins.php?Agent=<?php echo $Agent?>">View admins</a></li>
    <li class="border-right"><a href="emails.php?Agent=<?php echo $Agent?>">E-mails</a></li>
    <li class="right border-right"><a href="logout.php"><?php echo $Agent?></a>
        <ul class="drop1">
            <li><a href="earnings.php?Agent=<?php echo $Agent?>">Earnings</a></li>
            <li id="hover-trigger"><a href="#">Change Password</a>
                <ul class="drop2">
                    <li>
                        <form action="changepass.php?Agent=<?php echo $Agent?>" method="POST">
                            <input type="password" name="1" placeholder="Enter new password">
                            <input type="password" name="2" placeholder="Repeat new password">
                    </li>
                    <li>
                            <input type="submit" name="changepw" class="button" value="Change">
                        </form>
                    </li>
                </ul>
            </li>
        <li>
            <form action="logout.php">
                <input type="submit" class="button" value="Log out">
            </form>
        </li>
        </ul>
    </li>

But it doesn't work. It shows the active page as active, however, it shows 2 pages as simple hyperlinks. This is what it looks like:

Does anyone have any clue as to why this happens? It works for 2 but fails for 2, exact same code? Thanks

解决方案

You should do it like this:

<li <?php echo ($current_url == "globaluser.php") ? $active : ''?> ><a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li>

because right now you're conditioning the presence of the opening <li> tag

这篇关于用php显示当前页面的活动导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆