Highlight current page with included header file - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Tuesday, June 1, 2021

Highlight current page with included header file

I have a header file that I include with php on every page. Inside the navigation I use php to check if someone is logged in. Depending on the check the header looks different (different list items).

Following these guidelines (highlight the current page in menu in php) I am aware that a good way to achieve my goal is to declare a basename[PHP_SELF]-variable and then add some code to those list items.

But that is where I mess up. Since I already use php inside my html to check if the user is logged in or not I get lost in the single or double quotes I need to use to add the php code to my list items.

<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>

Could someone please help me out how my list items should look while being echoed? Thank you so much!



source https://stackoverflow.com/questions/67781310/highlight-current-page-with-included-header-file

No comments:

Post a Comment