Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP easier way to hide/show menu items to logged in / logged out users

Tags:

php

Is there an easier more efficient way to hide/show menu items to logged in, logged out users? It seems like I should not have to copy the whole menu again with duplicate menu items. The menu items maybe in different order like below

You can see in my example below I have added to links in the <?php } else { ?> statement

<?php
    if($_SESSION["loggedIn"] == "yes") { 
    ?>
    <ul class="nav navbar-nav">
      <li class="active"><a href="#">Home</a></li>
      <li><a href="retailers.php">Stores</a></li>
      <li><a href="coupons.php">Coupons</a></li>
      <li><a href="featured.php">Featured Offers</a></li>
      <li><a href="howitworks.php">How It Works</a></li>
      <li><a href="help.php">Help</a></li>
    </ul>

    <?php } else { ?>
        <ul class="nav navbar-nav">
          <li class="active"><a href="#">Home</a></li>
          <li><a href="retailers.php">Stores</a></li>
          <li><a href="coupons.php">Coupons</a></li>
          <li><a href="myaccount.php">My Account</a></li>//logged in item
          <li><a href="featured.php">Featured Offers</a></li>
          <li><a href="howitworks.php">How It Works</a></li>
          <li><a href="help.php">Help</a></li>
          <li><a href="myfavorites.php">My Favorite Stores</a></li>//logged in item
        </ul>
    <?php
    }
?>
like image 484
Michael Falciglia Avatar asked Dec 19 '22 20:12

Michael Falciglia


1 Answers

Updated for new ordering of items:

<ul class="nav navbar-nav">
  <li class="active"><a href="#">Home</a></li>
  <li><a href="retailers.php">Stores</a></li>
  <li><a href="coupons.php">Coupons</a></li>
  <?php
  if($_SESSION["loggedIn"] == "yes") { 
      echo '<li><a href="myaccount.php">My Account</a></li>';
  }
  ?><li><a href="featured.php">Featured Offers</a></li>
  <li><a href="howitworks.php">How It Works</a></li>
  <li><a href="help.php">Help</a></li>  
  <?php
  if($_SESSION["loggedIn"] == "yes") { 
      echo '<li><a href="myfavorites.php">My Favorite Stores</a></li>';
  }
  ?>  

</ul>
like image 68
Jessica Avatar answered Dec 24 '22 01:12

Jessica