PHPBuilder - PHP to generate Menu System



RSS Twitter
Tips Html

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 :

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

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):

<?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
$DEFAULT_FRAME = "RIGHT_FRAME";

// 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|www.xyz.com/moon.php?x=123|TOP_FRAME",
  "Star Page|www.geocities.com|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)
{
 global $DEFAULT_FRAME;

 // 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);
      $addr[0]=substr($on_off,$i,1)+0;
      $arr[key($arr)]=$addr;
      @next($arr);
      }
  }

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

 $i=0;
 reset($arr);
 while (list($k,$v)=each($arr)) 
     {
     $title = explode("|",$k);
     if (trim($title[1]))
        $title = "<IMG SRC='$title[1]' ALT='$title[0]'>";
     else
        $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'>".
              $title."</A>
\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|www.xyz.com/moon.php?x=123|TOP_FRAME",
  "Star Page|www.geocities.com|RIGHT_FRAME"),
menu1.gif      : the Picture of first main menu, if doesn't
                 exist, the "This is menu 1" will be the
                 title.
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
SYSTEM DEVELOPER
www.globalsources.com

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Alexander Yanuar Koentjara

Comment:



Comment:

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