How add class='active' to html menu with php

Posted by meow on Stack Overflow See other posts from Stack Overflow or by meow
Published on 2010-05-26T13:34:28Z Indexed on 2010/05/26 13:51 UTC
Read the original article Hit count: 244

Filed under:

Hello

I want to put my html navigation in a separate php file so when I need to edit it, I only have to edit it once. The problem starts when I want to add the class active to the active page.

I've got three pages and one common file.

common.php :

<?php 
     $nav = <<<EOD
        <div id="nav">
            <ul>
               <li><a <? if($page == 'one'): ?> class="active"<? endif ?> href="index.php">Tab1</a>/</li>
               <li><a href="two.php">Tab2</a></li>
               <li><a href="three.php">Tab3</a></li>
           </ul>
        </div>
    EOD;
 ?>

index.php : All three pages are identical except their $page is different on each page.

  <?php
     $page = 'one';      
     require_once('common.php');
    ?>
    <html>
       <head></head>
       <body>
          <?php echo $nav; ?>
       </body>
    </html>

This simply won't work unless I put my nav on each page, but then the whole purpose of separating the nav from all pages is ruined.

Is what I want to accomplish even possible? What am I doing wrong?

Thanks

EDIT: When doing this, the php code inside the li don't seem to run, it's just being printed as if it was html

© Stack Overflow or respective owner

Related posts about php