Click here to Skip to main content
15,892,005 members
Articles / Programming Languages / PHP
Tip/Trick

Dynamic Navigation Menus

Rate me:
Please Sign up or sign in to vote.
4.18/5 (13 votes)
14 Apr 2010CPOL 16.9K   8   1
Next code demonstrates how to build a simple menu function with the current page high-lighted.index.phpPage - .inactive,...
Next code demonstrates how to build a simple menu function with the current page high-lighted.

index.php
XML
<?php
require_once('menu.php');
$page = isset($_GET['page']) ? $_GET['page'] : 'home';
?>
<html>
<head>
<title>Page - <?=$page?></title>
<style type="text/css">
.inactive, .active
{
 padding:2px 2px 2px 20px;
}
.inactive
{
 background:#ddd;
}
.active
{
 background:black;
 font-weight:bold;
}
.inactive a
{
 text-decoration:none;
}
.active a
{
 text-decoration:none;
 color:white;
}
</style>
</head>
<body>
<table>
<tr>
<td width="200" valign="top">
<?php page_menu($page); ?>
</td>
<td width="600" valign="top">
Page: <?=$page?>
</td>
</tr>
</table>
</body>
</html>


menu.php
<?php
function menu_item($id, $title, $current)
{
  $class = ($current == $id) ? "active" : "inactive";
?>
  <tr><td class=<?=$class?>>
  <a href="index.php?page=<?=$id?>"><?=$title?></a>
  </td></tr>
<?php
}
function page_menu($page)
{
?>
  <table width="100%">
  <?php menu_item('home', 'Home', $page); ?>
  <?php menu_item('about', 'About', $page); ?>
  <?php menu_item('browse', 'Browse', $page); ?>
  <?php menu_item('download', 'Download', $page); ?>
</table>
<?php
}
?>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionphp dynamic menu Pin
chrisdavies2327-Nov-12 4:38
chrisdavies2327-Nov-12 4:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.