PHPBuilder - PHP to generate Menu System

RSS Twitter

PHP to generate Menu System

by: Alexander Yanuar Koentjara
December 14, 2000

Hi, this is (maybe) the simplest PHP code to generate menu system that can work both for Microsoft Internet Explorer and NetScape.

The features :

1. Targeted to multiple FRAME(s) The URL of menu can be targeted to any FRAME, or any WINDOW. 2. Opened/closed state of main menu. The main menu can be clicked to show/hide its sub menus without JavaScript needed. 3. Main menu can be replaced by image, or ordinary text.

What you have to do :

1. First, create a starting page for your menu. For example :

  <FRAME SRC="menux.php" NAME="MENU_FRAME">
  <FRAMESET ROWS="50%,*">
     <FRAME SRC="1.htm" NAME="TOP_FRAME">

Save this as index.html.

The menux.php is the code I want to show.
File 1.htm is starting page for TOP_FRAME.
File 2.htm is starting page for RIGHT_FRAME.
You can define as much FRAME you want, and then you can correspond it with menux.php

2. Now, this is to generate the menu (menux.php):


The Menu Hierarchy :

Main Menu Config
The title of menu|the picture of menu (if exist)

Sub Menu Config
The title of sub menu|the url of sub menu|the target frame

"|" is a symbol to seperate each parts


// The default of TARGET if not provided

// The Main Menus and their Sub Menus :
$all_menu = array(
"This is menu 1|menu1.gif" => array
 ( 0,
  "Sun Page|intro.html|RIGHT_FRAME",
  "Moon Page||TOP_FRAME",
  "Star Page||RIGHT_FRAME"),

"This is menu 2|menu2.gif" => array    
 ( 0,
  "<img src='xyz.gif'>|mn21.html|RIGHT_FRAME",  // using image ...
  "Hello Page|mn22.html|RIGHT_FRAME",
  "Any Page|mn23.html|TOP_FRAME"),

"This is menu 3|menu3.gif" => array  
 ( 0,
  "Lion Page|mn31.html|RIGHT_FRAME",
  "Swan Page|mn32.html|TOP_FRAME",
  "Tiger Page|mn33.html|RIGHT_FRAME",
  "Rabbit Page|mn34.html|TOP_FRAME")

function generate_menu($arr,$on_off)

 // this is to update the main menus whether in
 // opened or closed state
 if ($on_off)
  for ($i=0;$i<strlen($on_off);$i++)
      $addr = current($arr);

 $on_off = "";
 while (list($k,$v)=each($arr)) $on_off .= $v[0];

 while (list($k,$v)=each($arr)) 
     $title = explode("|",$k);
     if (trim($title[1]))
        $title = "<IMG SRC='$title[1]' ALT='$title[0]'>";
        $title = $title[0];

     // if main menu is opened, list its sub menus also
     if ($v[0])
        $temp = substr($on_off,0,$i)."0".substr($on_off,$i+1);
        print "<A Href='menux.php?on_off=$temp'>".
\n"; for ($j=1;$j<count($v);$j++) { $title = explode("|",$v[$j]); if (!trim($title[2])) $title[2]=$DEFAULT_FRAME; $temp = "TARGET='$title[2]'"; print "&nbsp;&nbsp;&nbsp;"; print "<A Href='$title[1]' $temp>". "$title[0]</a>
\n"; } } else // if main menu is closed { $temp = substr($on_off,0,$i)."1".substr($on_off,$i+1); print "<A Href='menux.php?on_off=$temp'>". $title."</A>
\n"; } $i++; } } print "<HTML>\n<BODY>\n"; generate_menu($all_menu, $on_off); print "</BODY>\n</HTML>\n"; ?>

Let's see what it means with the first main menu definition :

"This is menu 1|menu1.gif" => array
 ( 0,
  "Sun Page|intro.html|RIGHT_FRAME",
  "Moon Page||TOP_FRAME",
  "Star Page||RIGHT_FRAME"),
menu1.gif      : the Picture of first main menu, if doesn't
                 exist, the "This is menu 1" will be the
This is menu 1 : the <IMG ALT=""> of first main menu
0              : initial main menu condition. 
                 0 means closed,  
                 1 means opened.
Sun Page       : The title of first sub menu.
intro.html     : the url of the first sub menu.
RIGHT_FRAME    : the target frame, if doesn't exist, 
                 $DEFAULT_FRAME will be used.

Best Regards,

Alexander Yanuar Koentjara

Comment and Contribute

Your comment has been submitted and is pending approval.

Alexander Yanuar Koentjara



(Maximum characters: 1200). You have characters left.