Recommend this page to a friend!
  Classes of Justin Koivisto  >  phpTierMenu  >  >  Download  
Role: ???
Content type: text/plain
Description: Class definition file with examples at the end.
Class: phpTierMenu
A tree menu that has unlimited number of branches.
Author: By
Last change:
Date: 19 years ago
Size: 8,324 bytes


Class file image Download
	   This is my multi-tier menu system. The menu itself is printed into
	   a one-celled table. There are no collapse or expand functions, but
	   you have a high level of control for the rest of the looks. This has
	   great potential since I designed it in such a way to make it very
	   easy to use loops to set the data (especially if you store it in a
	   database like I do).

	   There is no limit to the number of tiers for this menu system, but
	   there are drawbacks also. For instance, if you specify your width
	   say as 150 pixels, and you use indent spacing, you'll have problems
	   with text wrapping or images cutting off. Text wrapping wouldn't be
	   so bad if it also justified itself after the line break.

	   One thing to note is the way the indents and spacing is done. I use
	   an image (1x1 pixel, transparent gif "images/spacer.gif") for all
	   the spacing, so you may want to either create that image, or find it
	   in the code below (lines 114, 160) to change it to your preference.

	   This is a great startting point for a more advanced menuing system.
	   What I have here serves my needs for now, so I don't think I'll be
	   doing a lot of work on it until it's needed. However, feel free to
	   make improvements and fix fea^H^H^H bugs. If you do, please send
	   me a copy!

	   Examples on use at the bottom of the file.

	   Last Modified by Justin Koivisto ( 3/21/01 6:42PM


class menu_item_class{
	var $text;	// text of this item
	var $url;	// link - unquoted for ability to add target info
	var $font_size;	// the size that the text will appear as (if any)
	var $font_face;	// the face that the text will appear as (if any)
	var $font_color;	// the color that the text will appear as (if any)
			// NOTE: for the color, you can either use the names
			// (like 'blue') or the hexadecimal value beginning
			// with a pound sign (#)
	var $image;	// file to use as an image link (if any)
	var $image_height;	// the height in pixels of above
	var $image_width;	// the width in pixels of above
	var $items;	// array of children objects (indexed by child text)
	var $num_items;	// the number of sub-items for this one

	// Constructor
	function menu_item_class($text,$url=''){

	// functions to set values
	function setFontFace($value){$this->font_face=$value;}
	function setFontSize($value){$this->font_size=$value;}
	function setFontColor($value){$this->font_color=$value;}
	function setImage($value){$this->image=$value;}
	function setImageSize($width,$height){

	// adding children to menu items
	function addSub($name,$url=''){
		$this->items[$name]=new menu_item_class($name,$url);

class menu_class{
	var $width;		// the width of the table
	var $spacer;		// the height between items in pixels
	var $indent;		// the width in pixels of sub-item's indent
	var $bgcolor;		// the background color of the row (if any)
	var $background;	// the background image of the row (if any)
	var $padding;		// the cellpadding of the table
	var $border;		// the width of the border around the table
	var $align;		// the horizontal alignment of the cell
	var $valign;		// the vertical alignment of the cell
	var $items;		// the children of the menu (indexed by name)

	// Constructor
	function menu_class($name,$width){

	// function to set table values
	function setSpacer($value){$this->spacer=$value;}
	function setIndent($value){$this->indent=$value;}
	function setColor($value){$this->bgcolor=$value;}
	function setBorder($value){$this->border=$value;}
	function setBackImage($value){$this->background=$value;}
	function setPadding($value){$this->padding=$value;}
	function setAlignment($horiz,$vert){$this->align=$horiz;$this->valign=$vert;}

	// how to add menu heading items
	function addItem($text,$url=''){
		$this->items[$text]=new menu_item_class($text,$url);

	// print an item menu
	function printItem(&$tmp,$tier){

		// we need to check if we need an indent
			$output.="<img src=images/spacer.gif width=";
			$output.="$indent height=1 align=left>";

		// now we need to add the link stuff
			$output.="<a href=$tmp->url>";

		// take care of the font stuff if set (otherwise we'll just
		// print an empty font tag - no big deal
			$output.=" face=$tmp->font_face";
			$output.=" size=$tmp->font_size";
			$output.=" color=$tmp->font_color";

		// if we are using images, show it
			$output.="<img src=$tmp->image";
				$output.=" width=$tmp->image_width";
				$output.=" height=$tmp->image_height";
			$output.=" border=0 alt=\"$tmp->text\">";
			// if not images, we need the text output then

		// close font tag

		// don't forget to close the anchor tag

		// add line break after each item

		// give the spacer a chance
			$output.="\n        <img src=images/spacer.gif width=1 height=$this->spacer><br>";

		// How about this item's children?
			foreach($tmp->items as $submenu){

		return $output;

	// print menu. returns HTML code from function rather than
	// printing directly to output
	function printMenu(){
		$output="\n\n<!-- BEGIN: $this->text -->\n";
		$output.=" <table width=$this->width cellspacing=0 cellpadding"
			."=$this->padding border=$this->border>\n  <tr";
			$output.=" background=$this->background";
			$output.=" bgcolor=$this->bgcolor";
		$output.=">\n   <td";
			$output.=" align=$this->align";
			$output.=" valign=$this->valign";

		// now we need to print the menu items
		foreach($this->items as $item)

		$output.="\n   </td>\n  </tr>\n </table>\n<!-- END: "
			."$this->text -->\n\n";
		return $output;

// -------------------------------- EXAMPLES ---------------------------------

// start by defining the menu's looks and name
$menu=new menu_class('Menu Title',175);

// now we will add in the menu items
$menu->addItem('Heading 1','1.htm'); // tier 1
$menu->items['Heading 1']->setFontSize(1);
$menu->items['Heading 1']->setFontFace("arial");
$menu->items['Heading 1']->setFontColor("#000000");
$menu->items['Heading 1']->addSub('Sub-heading','images/');
$menu->items['Heading 1']->addSub('Sub-heading0','images/');

$menu->addItem('Heading 2','2.htm'); // tier 1
$menu->items['Heading 2']->setImage("images/test.gif");
$menu->items['Heading 2']->setImageSize(125,20);

$menu->addItem('Heading 3','3.htm'); // tier 1
$menu->items['Heading 3']->addSub('1','images/');
$menu->items['Heading 3']->items['1']->setFontSize(1);
$menu->items['Heading 3']->items['1']->addSub('2','url');
$menu->items['Heading 3']->items['1']->items['2']->setFontSize(1);
$menu->items['Heading 3']->items['1']->items['2']->addSub('3','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->addSub('4','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->addSub('5','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->items['5']->addSub('6','url');
$menu->items['Heading 3']->items['1']->items['2']->items['3']->items['4']->items['5']->items['6']->addSub('7','url');

$menu->addItem('Heading 4','4.htm'); // tier 1

echo $menu->printMenu();


For more information send a message to info at phpclasses dot org.