Chezshire
Chezshire

Reputation: 713

php: Remove/show specific link from menu based on page displaying

Newb here working on refining a function to deliver a better user experience.

My goal is that if we are not on the index page (home) of the site, i wish to include a link to return to the home page. But if we are on the index page (Home), i don't wish to display the redundant link in the menu. Below is the function I've built, which you can see here 'school.max-o-matic.com'. at the bottom of the right navigation menu is a link that reads '< back' which should not show on the index page but should ideally show on all other pages.

I'd hoped the use of the exclamation point followed by an equal sign would do the trick but it did not.

<?php

function makeNav($navItem) {
//created variable - plops in what is called when function used on
//page calling the function itself - this is like the strPromp
    $output = ''; //Variable of 0 length
    foreach ($navItem as $pageLink => $displayedLink) {
        if ($pageLink == THIS_PAGE) {
//url matches page - add active class
            $output .='<li class="active">'
                    . '<a href="' . $pageLink . '">' . $displayedLink . '</a>'
                    . '</li>' . PHP_EOL;
        } //PHP_EOL is php line end for all systems
        else {//don't add class
            $output .='<li>'
                    . '<a href="' . $pageLink . '">' . $displayedLink . '</a>'
                    . '</li>';
        }
    }

    if ($pageLink != 'index.php') {//add back button to index page
        $output .='<li>'
                . '<a href="./index.php">< Back</a>'
                . '</li>' . PHP_EOL;
    } //PHP_EOL is php line end for all systems

    $output .='<li>'
            . '<a href="#contact">contact</a>'
            . '</li>';
    return $output;
}
?>

Upvotes: 0

Views: 70

Answers (2)

Plenka
Plenka

Reputation: 1139

You could check this by using the $_SERVER global variable, which is an array of request information. The key 'SCRIPT_NAME' contains the path to the requested file. The check on this could be:

if (basename($_SERVER['SCRIPT_NAME']) != 'index.php') {

Upvotes: 0

Vish
Vish

Reputation: 4492

if($_SERVER['PHP_SELF'] != '/index.php'){ //process if file is not index file.

}

to exclude access to index file.

if (strpos($_SERVER['PHP_SELF'],'index.php') !== false) { //process if file contains text "index.php" in filename.

}

//to exclude access to any file with name containing "index.php" file

if (basename($_SERVER['SCRIPT_NAME']) != 'index.php'){
 //run on all files that ARE NOT index files in any folders.
}

Upvotes: 2

Related Questions