Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-16-2012, 12:17 AM   PM User | #1
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Nav bar - disabling current page link

A lot of samples on this topic build a navigation bar element by element, checking each time if the link points to the current page. I think my approach is much simpler.

I create the navbar once as an HEREDOC string, and then do a simple string replace (str_replace) to inject an id (or class) for the current page. I can do the same for the main navigation-menu (for the current page). That is, the main category can appear differently formatted, to indicate it includes the current page.

I'm also reading session data to add a link for either 'Log In' or 'Log Out: user name'. This requires session_start() to have occurred earlier in the code.

PHP Code:
<?php
// The current page's link will have the id 'current' added. This is 
// made inactive with CSS. Similarly, the main navigation-group will 
// be given id 'currentgroup' - if there is a group.
$navbar = <<< EOT
<nav>
    <ul id="nav">
        <li class="level1"><a href="index.php">Home</a>
            <div id="navslider"></div></li>
        <li class="level1"><a href="html.php">HTML</a></li>
        <li class="level1"><a href="css.php">CSS</a>
            <ul>
                <li><a href="css10rules.php">10 Rules</a></li>
                <li><a href="cssbox.php">Box Model</a></li>
                <li><a href="cssposition.php">Positioning</a></li>
                <li><a href="css_coding.php">Code Paper</a></li>
            </ul>
        </li>
        <li class="level1"><a href="javascript.php">JavaScript</a>
            <ul>
                <li><a href="richtext.php">Rich Text</a></li>
                <li><a href="map.php">Map</a></li>
                <li><a href="another3.html">Another 3</a></li>
            </ul>
        </li>
        <li class="level1"><a href="login.php">Log In</a></li>
        <li class="level1"><a href="testarea/test.php">Test Area</a></li>
    </ul>
</nav>
EOT;

if (isset(
$_SESSION['agent']) && $_SESSION['agent'] == 
        
md5($_SERVER['HTTP_USER_AGENT'])) {
    
$navbar str_replace("login.php""logout.php"$navbar);
    if (isset(
$_SESSION['username'])) {
        
$navbar str_replace("Log In""Log Out: " $_SESSION['username'], $navbar);
    } else {
        
$navbar str_replace("Log In""Log Out"$navbar);
    }  
}

if (!isset(
$thispage) || empty($thispage)) {
    
// get the sub-domain(s) and page for the current page
    
$thispage curSubsAndPage();
}
$navbar str_replace("\"$thispage\"""\"#\" id=\"current\""$navbar);

if (isset(
$thisgroup) && !empty($thisgroup)) {
    
$navbar str_replace("\"$thisgroup\"""\"$thisgroup\" id=\"currentgroup\""
        
$navbar);
}

echo 
"\n$navbar\n";
?>
I can set the variable $thispage before including the navbar - for testing, or if there is some issue preventing the page-name being found. The variable $thisgroup needs to be set manually.

It also uses the following function to read the current sub-domains/page.

PHP Code:
function curSubsAndPage() {
    
// Returns the /subdomains/page.extn, with the leading '/' removed.
    
$locally strpos($_SERVER['PHP_SELF'], '/AndysProject2/');
    if (
$locally !== false) {
        return 
substr($_SERVER['PHP_SELF'], 15);
        
// .. return everything after '/AndysProject2/';
    
}
    if (
substr($_SERVER['PHP_SELF'], 01) == '/') {
        return 
substr($_SERVER['PHP_SELF'], 1);     // remove leading '/'
    
} else {
        return 
$_SERVER['PHP_SELF'];
    }    

I've included my local project-folder name within this function; this could be set as a variable to make it more flexible.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:19 PM.


Advertisement
Log in to turn off these ads.