1

I'm trying to build a personal website, (fairly new to HTML and PHP) and am having trouble building a dynamic menu bar. By dynamic, I mean that I want the page that the use is on be highlighted.

Right now I have a horizontal menu on my page (Home, Contact, etc...), and have the CSS set up so that when one of the links has the attribute class="active", then the item remains highlighted (so the user knows which page he or she is on).

For example, in a static HTML page I would copy and paste the following code to each other static page and just change where the class="active" attribute is to align with the proper page:

<a href="?page=home" class="active">Home</a>
<a href="?page=pagetwo">Page Two</a>
<a href="?page=contact">Contact</a>

I obviously want to use PHP to be able to minimize the amount of duplicated code I have scattered around.

So far, I have followed the first answer on this page It has worked great. I am able to click on a menu item and the content comes up in the body of the page. However:

  • I can't get it to dynamically highlight the menu since the menu options (the <a href /> lines) are not being dynamically created through PHP.
  • When I go to the page directly through index.php, I get an error:

Notice: Undefined index: 'page' in C:\xampp\htdocs\index.php on line 43

Obviously, when I go the page directly, the ?page=home variable is not set in the line:

<a href="?page=home">Home</a> 

So, the 'page' variable in GET has not been set. I've gotten around that with an if statement that checks if it is not set and sends the home page html. However, I don't think this is the best way to do it, and when I try to tackle part b), I'm thinking I need to change this completely. My entire PHP script is like this:

<?php

$current_page = 'home';
$pages = array('home', 'pagetwo', 'contact');

function setActiveHeader() {

  global $current_page;
  global $pages;
  $arr_length = count($pages);

  for($x=0;$x<$arr_length;$x++) {
    if($pages[$x] == $current_page) {
      echo "<a href=\"?page=$pages[$x]\" class=\"active\">$pages[$x]</a>";
    } else {
      echo "<a href=\"?page=$pages[$x]\">$pages[$x]</a>";
    }
  }


}

function putPage($page) {
    // put a list of allowed pages here
    global $pages;

    $page = trim($page);
    $page = (in_array($page, $pages)) ? $page : 'home';

    // set current page global variable
    $GLOBALS['current_page'] = $page;

    // output contents of page selected
    echo @file_get_contents('.\html\\' . $page . '.html');
}

?>

I just try to call setActiveHeader() from the HTML, but that doesn't work right. The menu is output correctly, but the the correct item doesn't get highlighted, it just stays stuck on the home option highlighted. Not sure why it is not being updated. Is there a better way to go about doing this?

2
  • you don't seem to have 43 lines, could you identify which line is line 43 for you? Commented Apr 14, 2013 at 3:17
  • Right, sorry, I was just using snippets of code, so the lines don't match up exactly. That line was the problem was: putPage($_GET['page']); Commented Apr 16, 2013 at 14:38

1 Answer 1

1

Your code only goes up to 37 lines, and we can't much without the line that the error is referencing, but I'll try my best.

Basically, what Undefined Index means, is that you're trying to access an element in an array that isn't set. I'm guessing that you're trying to access $pages['page'].

There are two ways to fix this. Add page to the $pages array on the fourth line:

pages = array('home', 'pagetwo', 'contact', 'page');

Or wrap the 43rd line with an if statement:

<?php

$pages = array('home', 'about');

if (isset($pages['page'])) {
    echo $pages['page'];
}

?>

However, a far easier method would be to use CSS:

home.php

<html>
    <head>
        <title>Foo</title>
    </head>

    <body id="home">
        <ul class="menu">
            <li id="link-home"><a>Home</a></li>
        </ul>
    </body>
</html>

style.css

body#home {
    // active link styling here
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, you are right about using CSS, but I guess I'm forcing myself to learn PHP, so I stuck with using that.
I ended up adding the following, based on your advice: if(isset($_GET['page']) && !empty($_GET['page'])) { $current_page = $_GET['page']; } To check if its set and not empty, then set it to what its supposed to be, otherwise it defaults to home.

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.