我正在尝试使用use $current_url = basename($_SERVER['PHP_SELF']);
来确定我所在的页面,考虑到我的导航(html)存储在php文件中并包含在每个页面中。这是我用来确定哪个导航选项应该处于活动状态的代码:
<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>
但它不起作用。它将活动页面显示为活动页面,但是,它显示2页作为简单的超链接。这就是它的样子:
有没有人知道为什么会这样?它适用于2,但是2,完全相同的代码失败? 感谢
答案 0 :(得分:4)
你应该这样做:
<li <?php echo ($current_url == "globaluser.php") ? $active : ''?> ><a href="globaluser.php?Agent=<?php echo $Agent;?>">Overview</a></li>
因为您现在正在调整开场<li>
标记
答案 1 :(得分:1)
您在错误的地方使用<?php } ?>
它应该是在
<a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li>
更新后的代码如下:
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/no-background.css">
<ul>
<?php
$current_url = basename($_SERVER['PHP_SELF']);
echo $current_url;
$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;?> > <a href="search.php?Agent=<?php echo $Agent?>">Add new client</a></li> <?php } ?>
<?php if ($current_url == "viewadmins.php") { ?>
<li <?php echo $active;?> > <a href="viewadmins.php?Agent=<?php echo $Agent?>">View admins</a></li><?php } ?>
<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>
答案 2 :(得分:0)
您还可以使用preg_replace()添加class="active"
ob_start();
echo '<ul>
<li><a href="page1.php">Page 1</a></li>
<li><a href="page2.php">Page 2</a></li>
</ul>';
$output = ob_get_clean();
$pattern = '~<li><a href="'.$url.'">~';
$replacement = '<li class="active"><a href="'.$url.'">';
echo preg_replace($pattern, $replacement, $output);