Making menu from static to dynamic

hey folks, i have a menu of website. which is now being a problem for updating it. i can’t include it using php include coz in every section the a:active of li changes its color. so is there anyway that my menu stays same but somehow its dynamic.
here is the code

 <ul id="sidemenu"> 
                    <li id="active"><a href="">Home</a></li> 
                    <li><a href="">About us</a></li> 
                    <li><a href="">E</a></li> 
                    <li><a href="">Contact</a></li> 
                    <li><a href="">Sitemap</a></li> 
                    <li><a href="">Consumables</a></li> 
                    <li><a href="">Work Orders</a></li> 
                </ul> 

Are you using some kind of CMS or is this a completely custom website?

There are better ways to do it, but for a PHP newbie (like your sig says) I would recommend this:

  1. put your menu in a PHP file to be included with PHP. Modify it to look like this:
<ul id="sidemenu">
<?php
$menu = Array('putURLhere'=>'Home',
  'putURLhere'=>'About us',
  'putURLhere'=>'E',
  'putURLhere'=>'Contact',
  'putURLhere'=>'Sitemap',
  'putURLhere'=>'Consumables',
  'putURLhere'=>'Work Orders'
);

foreach($menu as $url=>$name) {
  $id = '';
  if($url == $ACTIVEURL)
    $id = ' id="active"';
  print "<li$id><a href=\\"$url\\">$name</a></li>\
";
}
?>
</ul>
  1. Where the menu goes for each page, put this code:
<?php
$ACTIVEURL='TheURLtoThisPage';
include('mymenu.php');
?>

Make sure the $ACTIVEURL matches the URL used in the menu EXACTLY and it will put the id=“active” on the right menu item.

PHP is capable of more automatic methods, but for a beginner, I’d start with this.

that was advance for me , but i know u have delcared a Array and passed in arguments which than runs a loop and sees which menu is clicked and assigned the id current. hope theoretical i said it right?

You’ve got the right idea. I’ll explain the code line by line to make it more clear:


<ul id="sidemenu"> <!-- this is just HTML, no PHP here -->
<?php
// create an array and insert the Key & Value pairs listed here
//            |--  KEY --|  |-value-|
$menu = Array('putURLhere'=>'Home',
  'putURLhere'=>'About us',
  'putURLhere'=>'E',
  'putURLhere'=>'Contact',
  'putURLhere'=>'Sitemap',
  'putURLhere'=>'Consumables',
  'putURLhere'=>'Work Orders'
);
// $menu now contains the array of all your menu item URLs and names
// each item MUST have a unique URL in this case.
// if 2 have the same URL, only the second one will be in your list because it has the same key as the first.

// this foreach will walk through your array
// the code in the { } will run for each entry in your array
// $url will be the key, and $name will be the value of the entry
foreach($menu as $url=>$name) {
  // the non-active entries will have no id, so $id will be an empty string
  $id = '';

  // if the url in the menu is the same as the variable $ACTIVEURL
  // then we will set $id
  if($url == $ACTIVEURL)
    $id = ' id="active"';

  // this will create the HTML you had before, inserting $url, $name
  // and adding ' id="active"' if this is the active item
  // echo is a synonym for print (you can use either)
  print "<li$id><a href=\\"$url\\">$name</a></li>\
";
}
?>
</ul> <!-- some more plain HTML to close the list -->

Now the code in the pages:

<?php
// this is a global variable we are creating
// with the url to compare to the menu items in the array
$ACTIVEURL='TheURLtoThisPage';

// this will display the HTML and execute the code we wrote above
// it's as if you pasted the code right here, only you don't have to
// mymenu.php will be able to use the global variable we just created
include('mymenu.php');
?>

Hope that explains everything. Good luck.

<?php
// this is a global variable we are creating
// with the url to compare to the menu items in the array
$ACTIVEURL=‘TheURLtoThisPage’;

what should i be filling theurltothispage?

$ACTIVEURL should be what is in the href field of the menu item that links to that page. For example, if you are on the contact page, and in the menu, the link to that page is <a href=“/contactus.php”>Contact Us</a>, then you want $ACTIVEURL = “/contactus.php”;

Ah, ok! thanks. .

ok here is another question. every module in my website has its own folder. so how to declare the variable in the page. suppose i have a About folder and in it is index.php

Assuming the link to your about page is <a href=“/about/”>About</a>, then your about page would look like this:

about/index.php:

<html>
<head>...</head>
<body>
  ...
  <?php
    $ACTIVEURL='/about/';
    include('mymenu.php');
  ?>
  ...
</body>
</html>

If it were a page like /about/ourlocation.php and “Our Location” was NOT in the menu, then you would want the about link to be the active link. that would look like this:

about/ourlocation.php:

<html>
<head>...</head>
<body>
  ...
  <?php
    $ACTIVEURL='/about/';
    include('mymenu.php');
  ?>
  ...
</body>
</html>

However, if “Our Location” was in the menu with a link of /about/ourlocation.php, then you would do this instead:

about/ourlocation.php:

<html>
<head>...</head>
<body>
  ...
  <?php
    $ACTIVEURL='/about/ourlocation.php';
    include('mymenu.php');
  ?>
  ...
</body>
</html>

so what u mean is that the same link path i gave in the file which was to be included like sidemenu.php would be the same? coz i m getting error which says
failed to open stream.no such file or directory in c:\wamp\myweb\about\ est.php