Extremely Simple Menu Generator

ShawnCplus 0 Tallied Votes 725 Views Share

This is about as simple as it gets with menu generators. Everything is explained in the code. Note that the menu is simply an unordered list so to pretty it up it really relies on the CSS.

If you don't want to write any CSS of your own here is a sample stylesheet.

#Menu 
{
	position:absolute;
	width:95%;
	height:44px;
	z-index:3;
	left: 4px;
	top: 175px;
} .tabbedmenu
{
    padding: 3px 0;
    margin-left: 0;                
    font: bold 12px Verdana;
    border-bottom: 2px groove #F16C0A;
    list-style-type: none;
    text-align: left;
}
.tabbedmenu ul 
{
    margin: 0;
    padding: 0;
    float: left;
}
.tabbedmenu li
{
    display: inline;
    position: relative;
    margin: 0;
}
.tabbedmenu li li{display: block;}
.tabbed menu li li:hover{display: inline;}

.tabbedmenu li a
{
    text-decoration: none;
    padding: 3px 7px;
    margin-right: 3px;
    border: 3px double gray;
    border-bottom: none;
    background-color: #000000;
    color: #FFFFFF;
}    
.tabbedmenu li a:hover
{
    background-color: #F16C0A;
    color: black;
}
.tabbedmenu li a:active {color: #FFFFFF;}
.tabbedmenu li a.selected
{
    position: relative;
    top: 1px;
    padding-top: 4px;
    background-color: #F16C0A;
    color: white;
}
<?php
/* 
 * @brief    print the menu to the page
 * @param string selected What is the current page?
 */
function writeMenu ($selected="index")
{
        /* the only tedious part is that a boolean variable has to be made
         * for each new page and a case put in the switch */
	$index   = 
	$staff   = 
	$contact = 
	$links   = false;
	$sel = 'class="selected"'; //change the class for selected page here
	switch($selected)
	{
		case 'staff':
			$staff = true;
			break;
		case 'links':
			$links = true;
			break;
		default: //fallthrough
		case 'index':
			$index = true;
			break;
	}
	// add pages in this array
	$pages = array("index", "staff", "links");   
	
//Change the div ID and the list class here
	echo '<div id="Menu">'."\n\t".'<ul class="tabbedmenu">';
	
	/* 
	 * This loop will generate a link on the menu.
	 * Page name is automatically generated by the link.
	 * IE: pages.php will show up as Pages on the menu
	 * (can be overridden as shown below by explicitly
	 * setting the value).
	 */
	 
	for($i=0;$i<count($pages);$i++)
	{
		$html = "\n\t\t".'<li><a href="'.$pages[$i].'.php" ';
		$html .= ($$pages[$i]==true)?$sel:""; 
		$pages[0]= "home"; //pagename override
		$html .='>'.ucfirst($pages[$i]).'</a></li>';
		echo $html;
	}
	$html .= '</ul>'."\n".'</div>'."\n";
	echo $html;
}
?>
emon 0 Newbie Poster

sd sdf sdf sdf ds

ayesha789 7 Posting Pro in Training

Its not working.....

TonyAR 0 Newbie Poster

You need to change this line from this:

$html .= '</ul>'."\n".'</div>'."\n";

To this:

$html = '</ul>'."\n".'</div>'."\n";

That stops the last menu item being duplicated.

And to display the menu put:

writeMenu();

As the 2nd last line of your script.

You could also include the menu.php as part of your main page, and call writeMenu(); there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.