Login

My PHP Digest

PHP Controlled Menu System

5th May 2007

Let PHP control what page your on, and change the menu background image to the page your on, in this tutorial it shows you the code that I use on this website. It will add id="selected" to the menu button of the page your on.

The Code


<?php
function CompileMenu(){
        
            
$value str_replace(array('/','.php'),'',$_SERVER['PHP_SELF']);
            
$link1 = array('<li>index</li>','<li>products</li>','<li>tutorials</li>','<li>contact</li>');
            
$replace = array('<li>','</li>');
            
    
$str NULL;
            
            foreach (
$link1 as $link){
            
                
$link str_replace($replace,'',$link);
        
                if(
$link==$value){ 
                    
$str .= '<li id="selected"><a href="'.$link.'" title="'.ucfirst($link).'">'.ucfirst($link).'</a></li>'
                } else {
                    
$str .= '<li><a href="'.$link.'" title="'.ucfirst($link).'">'.ucfirst($link).'</a></li>';    
                }
    
            }
            
            return 
$str;

 }
?>

Helpful Tips

Example on how i have done this is, ill have the page

contact.php

So i will add to $link1

<li>contact</li>

The code will then do the rest, as well as make it a capital at the start.

© 2006 John's PHP Digest. All Rights Reserved