0

I have PHP code which will check if the user is logged in and will return the menu if they are, however I was wondering if there was a way to make each of the current selected highlighted or would I have to go through and add them as a manual list to each page?

The code is:

<?php

if (!securePage($_SERVER['PHP_SELF'])){die();}

//Links for logged in user

if(isUserLoggedIn()) {

    echo "<div id='Default'>
    <ul>
    <li><a href='/account.php' >Account Home</a></li>
    <li><a href='/user_settings.php' >User Settings</a></li>
    <li><a href='/logout.php' >Logout</a></li>
    </ul></div>

    <div id='button1'>
    <a href='/Demos.php'>Demos</a></div>
    <div id='button2'>
    <a href='/Helpfiles.php'>Helpfiles</a></div>


    <div id='greeting'>
    Hello, $loggedInUser->displayname.</br>";



} 

//Links for users not logged in
else{
    echo "<div id='Default'>
    <ul>
    <li><a href='/login.php'>Login</a></li>
    <li><a href='/register.php'>Register</a></li>
    <li><a href='/forgot-password.php'>Forgot Password</a></li>";

    echo "</ul></div>";
}

?>

Now I know that on a normal CSS one it would just be .current and you can do it that way, however I cannot make that work with this echo because they are all on the screen at the same time. What would be the best way? Manually add would see like the longer way.

p.s. this is used in conjunction with usercake

Marriott81
  • 275
  • 2
  • 16

3 Answers3

1

You don't need to put HTML into an echo in PHP. I would recommend something like this. So you will end up with something like:

<?php
if(isUserLoggedIn()) {
    ?>  
    <div id='Default'>
    <ul>
      <li><a href='/account.php' >Account Home</a></li>
      <li><a href='/user_settings.php' >User Settings</a></li>
      <li><a href='/logout.php' >Logout</a></li>
    </ul>
    </div>

    <div id='greeting'>
      Hello, <?php echo $loggedInUser->displayname; ?>
      </br>
<?php } ?>

Then I would not recommend you to add the class with PHP because you will suffer from lisibility with a lots of if and else cases.

The best way to do this would be to use ID/Classes for your LIs and add the selected class to a specitic item with a simple JavaScript function.

Btw, if you really feel the needs to have this in PHP I recommend you to read this: http://www.catswhocode.com/blog/snippets/highlight-current-menu-item-in-php http://webdeveloperswall.com/php/how-to-highlight-the-current-page-in-menu-in-php

So you will have something like:

<?php if(isUserLoggedIn()) { ?>  
<ul>
    <?php
    $url = $_SERVER['REQUEST_URI'];
    $parts = parse_url($url);
    $page_name = basename($parts['path']);
    ?>
    <li><a class="<?php echo ($page_name=='acount.php')?'selected':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
    <li><a class="<?php echo ($page_name=='user_settings.php')?'selected':'';?>" href="about.php">ABOUT US</a></li>
    <li><a class="<?php echo ($page_name=='logout.php')?'selected':'';?>" href="contact.php">CONTACT US</a></li>
</ul>
<?php } ?>

EDIT Finally, you should end up with something like this: http://pastebin.com/V8jxwi7T

Yves Lange
  • 3,914
  • 3
  • 21
  • 33
  • Will try now, it was written by usercake which is horrible code, but got what i needed done. – Marriott81 Feb 18 '14 at 12:06
  • check out my update, I like the code but it keeps breaking because of the full code I think, basically I call this file on each page to display the correct heading – Marriott81 Feb 18 '14 at 12:21
  • I wrote what you should have. Complete code is in my post last line or here : http://pastebin.com/V8jxwi7T – Yves Lange Feb 18 '14 at 13:18
0

You could grab the page you are currently browsing and see if it matches. Something like this perhaps? (sorry for the crude example)

<?php
// will return 'home' is the filename is home.php
$filename       = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
?>
<li <?php if ($filename == "home") { echo "class='active'"; } ?>>
    <a href="home.php">Home</a>
</li>

Some info on pathinfo and how it works here

stckrboy
  • 379
  • 10
  • 16
-1

Just add class to selected <li> element, this way:

echo "<div id='Default'>
<ul>
<li><a href='/account.php' >Account Home</a></li>
<li class="selected"><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";

In your CSS, put appropriate style for .selected class.

You should have a variable telling in which page you are, let's say it's $currentpage.

echo "<div id='Default'>
<ul>
<li".($currentpage=='account' ? ' class="selected"' : '')."><a href='/account.php' >Account Home</a></li>
<li".($currentpage=='usersettings' ? ' class="selected"' : '')."><a href='/user_settings.php' >User Settings</a></li>
<li><a href='/logout.php' >Logout</a></li>
</ul></div>";
Ghigo
  • 2,312
  • 1
  • 18
  • 19
  • my problem is that would only affect the one, I want it to change per page i.e. when on account its highlighted then different ones etc. p.s. wasn't me who downvoted – Marriott81 Feb 18 '14 at 11:59
  • Of course you know on which page you are. Add `class="selected"` to proper page. – Ghigo Feb 18 '14 at 12:03