【问题标题】:Highlight current page with included header file使用包含的头文件突出显示当前页面
【发布时间】:2021-08-19 04:58:09
【问题描述】:

我有一个头文件,我在每个页面上都包含了 php。在导航内部,我使用 php 检查是否有人登录。根据检查,标题看起来不同(不同的列表项)。

遵循这些准则 (highlight the current page in menu in 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>

有人可以帮我弄清楚我的列表项在被回显时应该是什么样子吗?非常感谢!

【问题讨论】:

  • 不要像这样回显 HTML。有关一些替代方案,请参阅here。甚至不清楚为什么你在 PHP echo 语句中有这个。其中没有 PHP。
  • @miken32,你可以这样echoHTML。没关系(虽然双引号会被解析,所以它们需要更长的时间来执行)。 OP 应该在发送任何标头之前 session_start(); 并学习 AJAX。
  • @StackSlave 你可以构造几千个chr()调用来输出HTML。这不合理。
  • @StackSlave 你可以,但这并不意味着你总是应该。尤其是在这里,OP 说这会给他们带来麻烦(“我迷失在单引号或双引号中”),试图最小化文本/html 的静态(或大部分静态)块的输出通过回声,是一个非常好的主意。

标签: php html navbar


【解决方案1】:

我没有意识到我可以在 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>

谢谢大家!

【讨论】:

    猜你喜欢
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    相关资源
    最近更新 更多