2

I have the code below in all of my pages, except for each page, the class is on that specific page.

<li><a href="../index.html">Homescreen</a></li>
<li><a href="socialstuff.html">Social Stuff</a></li>
<li><a href="sops.html">SOP's</a></li>
<li><a href="login.php">FTO Documents</a></li>
<li><a class="selected" href="changelog.html">Changelog</a></li>

I am working on throwing these inside of an include folder, that way each page I create does not have to have this line of code in each page. My only question is, how do I detect what page I'm on, and then automatically remove the old class slected and add the new one to the current page?

1
  • How did you go with this? Commented Apr 2, 2016 at 10:38

3 Answers 3

1

That largely depends on how you are handling routing. Assuming you are using just file name routing, then you can use a URI check.

<li><a href="socialstuff.html" class="<?php if($_SERVER['REQUEST_URI']) == 'socialstuff.html'){ echo 'selected'; } ?>">Social Stuff</a></li>

You would then do this for each line in your <ul> element.

Also, in the if($_SERVER['REQUEST_URI']) == 'socialstuff.html') line, the 'socialstuff.html' needs to be the exact URI that the address bar reads when you are on that page. So if socialstuff.html resides in a folder called misc, and the URI for that page would probably be misc/socialstuff.html.

Sign up to request clarification or add additional context in comments.

2 Comments

So how do I change said class to "selected"
I just changed it. You would put the class you want applied inside the echo argument.
1

One way that I tackle this is with a helper function.

function currentPage($page){

    if (basename($_SERVER['PHP_SELF']) == $page) {
        echo 'selected';
    }

}
        <li><a class="<?php currentPage('changelog.html') ?>" href="changelog.html">Changelog</a></li>

You will have to add this function to every li that you want to check if its that page

I havent tested the code, but you will get a general idea of how to handle this type of thing. Hope that helps.

1 Comment

This works! What I did was put this function inside my sidebar file. Works wonderful!
0

Taking the details from BayssMekanique's answer but making a more systematic response... Iterating through your links would help. I would also create a function renderLinkList() or similar, and return the string. Hope this helps.

<?php 
   $links = '';

$links = array(
    'Homescreen'   => '../index.html',
    'Social Stuff' => 'socialstuff.html',
    'SOP\'s'        => 'sops.html',
    'FTO Docs'     => 'login.php',
    'Change Log'   => 'changelog.html'
);

for each ($links as $title => $href) { 
    $selected = '';
    if ($_SERVER['REQUEST_URI']) == $href) {
        $selected = 'class="selected"';
    }
    $linkString  = '<li>';
    $linkString .= '<a href="'.$href.'" '.$selected.' >';
    $linkString .= $title.'</a></li>';
    $links .= $linkString;
}

//if you made a function you would now return links
print $links;

1 Comment

You should clear the $selected variable after appending it to the $links variable. Else it'd apply the same class to all subsequent links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.