php - 使用包含的头文件突出显示当前页面
问题描述
我有一个头文件,我在每个页面上都包含 php。在导航内部,我使用 php 检查是否有人登录。根据检查,标题看起来不同(不同的列表项)。
遵循这些准则(在 php 菜单中突出显示当前页面)我知道实现我的目标的一个好方法是声明一个 basename[PHP_SELF] 变量,然后向这些列表项添加一些代码。
但这就是我搞砸的地方。因为我已经在我的 html 中使用 php 来检查用户是否登录,所以我迷失在单引号或双引号中,我需要使用将 php 代码添加到我的列表项中。
<header>
<nav class="nav collapsible">
<ul class="list nav__list collapsible__content">
<?php
echo "<li class='nav__item'><a href='contact.html'>Contact</a></li>";
echo "<li class='nav__item'><a href='about.html'>About</a></li>";
?>
</ul>
<a class="nav__brand" href="index.html"><img class="link__logo" src="images/logoSmall.png" /></a>
<svg class="icon icon--white nav__toggler">
<use xlink:href="images/sprite.svg#menu"></use>
</svg>
<title class="header__title">My Page</title>
<ul class="list nav__list collapsible__content">
<?php
if (isset($_SESSION["userid"])) {
echo "<li class='nav__item'><a href='index.html'>Home</a></li>";
echo "<li class='nav__item'><a href='shop.php'>Shop</a></li>";
echo "<li class='nav__item'><a href='shoppingCart.html'>Shopping Cart</a></li>";
echo "<li class='nav__item'><a href='includes/logout.inc.php'>Log Out</a></li>";
}
else {
echo "<li class='nav__item'><a href='index.html'>Home</a></li>";
echo "<li class='nav__item'><a href='signup.php'>Sign Up</a></li>";
echo "<li class='nav__item'><a href='login.php'>Log In</a></li>";
}
?>
</ul>
</nav>
有人可以帮我弄清楚我的列表项在被回显时应该是什么样子吗?太感谢了!
解决方案
我没有意识到我可以在 if 条件之后关闭 php 语句,然后编写 html 代码。@miken32,这是你给我的一个非常有用的链接,非常感谢。
在没有所有引用内容的情况下重写我的代码后,很容易遵循我的 OP 中链接中的指南,并且它完全有效:
<header>
<nav class="nav collapsible">
<ul class="list nav__list collapsible__content">
<li class='nav__item <?= ($activePage == 'contact.html') ? 'active':''?>'><a href='contact.html'>Contact</a></li>
<li class='nav__item <?= ($activePage == 'about.html') ? 'active':''?>'><a href='about.html'>About</a></li>
</ul>
<a class="nav__brand" href="index.html"><img class="link__logo" src="images/logoSmall.png" /></a>
<svg class="icon icon--white nav__toggler">
<use xlink:href="images/sprite.svg#menu"></use>
</svg>
<ul class="list nav__list collapsible__content">
<?php
if (isset($_SESSION["userid"])) { ?>
<li class='nav__item <?= ($activePage == 'index.html') ? 'active':''?>'><a href='index.html'>Home</a></li>
<li class='nav__item <?= ($activePage == 'shop.php') ? 'active':''?>'><a href='shop.php'>Shop</a></li>
<li class='nav__item <?= ($activePage == 'shoppingCart.html') ? 'active':''?>'><a href='shoppingCart.html'>Shopping Cart</a></li>
<li class='nav__item <?= ($activePage == 'logout.php') ? 'active':''?>'><a href='includes/logout.inc.php'>Log Out</a></li>
<?php }
else { ?>
<li class='nav__item <?= ($activePage == 'index.html') ? 'active':''?>'><a href='index.html'>Home</a></li>
<li class='nav__item <?= ($activePage == 'signup.php') ? 'active':''?>'><a href='signup.php'>Sign Up</a></li>
<li class='nav__item <?= ($activePage == 'login.php') ? 'active':''?>'><a href='login.php'>Log In</a></li>
<?php }
?>
</ul>
</nav>
</header>
谢谢你们!
推荐阅读
- informix - PHP7.0 & Informix 数据库连接
- java - 如何将一个方法的返回语句用作另一个方法的参数?
- javascript - .signInWithEmailandPassword 不是函数
- jasper-reports - jasper Excel 报告中动态更改单元格颜色
- arm - ARM Cortex-A53 中的性能监控单元数量
- python - 几秒钟后,RPi 伺服停止响应 python 代码
- apollo - vue-apollo 突变触发意外订阅
- c# - 无法使用 mongocsharpdriver 2.7.0 连接到 MongoDb(使用身份验证)
- javascript - 对于手机等小屏幕尺寸,如何确保 ul 中的每个 li 具有相同的高度?
- firebase - 如何使用 .net Mvc 在 FireBase 中创建新项目