-2

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!

sluca
  • 1
  • 2
  • Don't echo HTML like this. See [here](https://stackoverflow.com/questions/1100354/how-can-i-echo-html-in-php) for some alternatives. It's not even clear why you have this in PHP `echo` statements. There's no PHP in them. – miken32 Jun 01 '21 at 00:11
  • @miken32, you can `echo` HTML like that. It's fine *(although double quotes do get parsed, so they take longer to execute)*. OP should `session_start();` before any headers are sent and learn AJAX though. – StackSlave Jun 01 '21 at 00:20
  • @StackSlave you *can* also construct a few thousand `chr()` calls to output HTML. It doesn't make it reasonable. – miken32 Jun 01 '21 at 00:25
  • @StackSlave you _can_, but it does not mean you always _should_. And especially here, where OP is saying this gets them into trouble (_“I get lost in the single or double quotes”_), trying to minimize outputting of static (or mostly static) blocks of text/html via echo, is a very good idea. – CBroe Jun 01 '21 at 09:03

1 Answers1

0

I did not realize I could close the php statement after the if condition and then write html-code. @miken32, that was a very useful link you gave me, thank you very much.

After rewriting my code without all the quote stuff it was quite easy to follow the guidelines in the link in my OP and it totally works:

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

Thank you all!

sluca
  • 1
  • 2