Dynamic html menu using php
<p>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.</p>

<p>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 <code>class="active"</code>, then the item remains highlighted (so the user knows which page he or she is on).</p>

<p>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 <code>class="active"</code> attribute is to align with the proper page:</p>

<pre><code>&lt;a href="?page=home" class="active"&gt;Home&lt;/a&gt;
&lt;a href="?page=pagetwo"&gt;Page Two&lt;/a&gt;
&lt;a href="?page=contact"&gt;Contact&lt;/a&gt;
</code></pre>

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

<p>So far, I have followed the first answer on this <a href="http://stackoverflow.com/questions/3329847/use-common-template-for-all-pages-of-website">page</a> 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:</p>

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

<blockquote>
  <p>Notice: Undefined index: 'page' in C:\xampp\htdocs\index.php on line 43</p>
</blockquote>

<p>Obviously, when I go the page directly, the <code>?page=home</code> variable is not set in the line:</p>

<pre><code>&lt;a href="?page=home"&gt;Home&lt;/a&gt; 
</code></pre>

<p>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:</p>

<pre><code>&lt;?php

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

function setActiveHeader() {

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

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


}

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');
}

?&gt;
</code></pre>

<p>I just try to call <code>setActiveHeader()</code> 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?</p>
