title: How to properly build a navigation menu that highlights the current page
body: <p>I've setup a menu for a fairly simple site based on <a href="http://icant.co.uk/sandbox/osx/" rel="nofollow noreferrer">icant.co.uk</a>.  It's fairly simple with maybe 5 pages.  The small site is mainly a mysql browser for a few tables using <a href="http://www.mysqlajaxtableeditor.com" rel="nofollow noreferrer">MATE</a>.  Theres a common.php file that contains the header &amp; footer HTML so thats where I put the code below.</p>
  
  <p>The code below highlights the current page on the menu.  Its ugly and I'm sure there has to be a better way to do it.</p>
  
  <p>Any help is appreciated, thank you!</p>
  
  <p>heres my code</p>
  
  <pre><code>&lt;?php
          $currentFile = Explode('/', $_SERVER["PHP_SELF"]);
          $currentFile = $currentFile[count($currentFile) - 1];
  
          if ($currentFile == "orders.php"){
                  echo '&lt;li id="active"&gt;&lt;a href="orders.php" id="current"&gt;Orders&lt;/a&gt;&lt;/li&gt;';
          }
          else{
                  echo '&lt;li&gt;&lt;a href="orders.php"&gt;Orders&lt;/a&gt;&lt;/li&gt;';
          }
  
          if ($currentFile == "customers.php"){
                  echo '&lt;li id="active"&gt;&lt;a href="customers.php" id="current"&gt;Customer List&lt;/a&gt;&lt;/li&gt;';
          }
          else{
                  echo '&lt;li&gt;&lt;a href="customers.php"&gt;Customer List&lt;/a&gt;&lt;/li&gt;';
          }
  
          if ($currentFile == "order_details.php"){
                  echo '&lt;li id="active"&gt;&lt;a href="order_details.php" id="current"&gt;Order Details&lt;/a&gt;&lt;/li&gt;';
          }
          else{
                  echo '&lt;li&gt;&lt;a href="order_details.php"&gt;Order Details&lt;/a&gt;&lt;/li&gt;';
          }
  ?&gt;
  </code></pre>
  
  <p><strong>UPDATE</strong> For those curious, below is the working code!  </p>
  
  <pre><code>&lt;?php
      $currentFile = Explode('/', $_SERVER["PHP_SELF"]);
      $currentFile = $currentFile[count($currentFile) - 1];
  
      // easier to manage in case you want more pages later
      $pages = array(
          array("file" =&gt; "orders.php", "title" =&gt; "Orders"),
          array("file" =&gt; "order_details.php", "title" =&gt; "Order Details"),
          array("file" =&gt; "customers.php", "title" =&gt; "Customer List")
      );
      $menuOutput = '&lt;ul&gt;';
      foreach ($pages as $page) {
         $activeAppend = ($page['file'] == $currentFile) ? ' id="active"' : "";
         $currentAppend = ($page['file'] == $currentFile) ? ' id="current' : "";
         $menuOutput .= '&lt;li' . $activeAppend . '&gt;'
                     .  '&lt;a href="' . $page['file'] . '"' . $currentAppend . '"&gt;' . $page['title'] .'&lt;/a&gt;'
                     .  '&lt;/li&gt;'; 
      }           
      $menuOutput .= '&lt;/ul&gt;';
  
      echo $menuOutput;
  </code></pre>
  
  <p>?></p>