NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 Menu Objects

Joseph Crawford codebowl at gmail.com
Tue Feb 8 20:27:49 EST 2005


Hello Everyone,

I have been working with PHP 5 for a while now and i have attempted to
create a Menu Object that i will extend at a later time to have one
object load and hold all of my site menu's throught a session.

The problem i am having is this

$et->Menu()->Group( "Group1" )
$g1index = $et->Menu()-GetIndex( "Group1" );

$et->Menu()->Group( $g1index )->Item( )->Title( "Item1" );
$i1index = $et->Menu()->Group( $g1index )->GetIndex( $i1index );

/* this should go through and set the link
   for the correct item.
*/
$et->Menu()->Group( $g1index )->Item()->Title( $i1index )->Link( "page.php" );

$et->Menu()->Draw();

it is not drawing the menu, and i dont think the correct items are
getting set as they should be.

the code for the classes you will find below, the reason i am doing
this is so that i can eventually implement a user system and have the
menu's either dynamically generated from database content and also
hide or show certain menu's for different user levels.







class Menu {
	static private $_groups;
	
	public function __construct() {
		$this->_groups = array();
	}
	
	public function GetIndex( $title ) {
		foreach( self::$_groups as $key => $group ) {
			if( strtolower( $group->Title() ) == strtolower( $title ) ) {
				return $key;
			}	
		}
		return -1;
	}
	
	static public function Group( $index = 0 ) {
	
		if( ( $index == 0 ) && ( count( self::$_groups ) != 0 ) ) $index =
count( self::$_groups );
		if( self::$_groups[$index] == null ) {
			self::$_groups[$index] = new MenuGroup();
		}
		return self::$_groups[$index];
	}
	
	public function Draw() {
		foreach( $this->_groups as $gkey => $group ) {
			echo $group->Title().'<br>';
			foreach( $group->_items as $ikey => $item ) {
				echo '&nbsp; &nbsp;'.$item->Title().'<br>';
			}
		}	
	}
}




class MenuGroup {
	static private $_items;
	private $_order;
	private $_title;
	
	public function __construct( ) {
		self::$_items = array();
	}
	
	public function Title( $var = "" ) {
		if( $var != "" ) {
			$this->_title = $var;
		}
		return $this->_title;
	}
	
	public function GetIndex( $title ) {
		foreach( self::$_items as $key => $item ) {
			if( strtolower( $item->Title() ) == strtolower( $title ) ) {
				return $key;
			}	
		}
		return -1;
	}
	
	static public function Item( $index = 0 ) {
	
		if( ( $index == 0 ) && ( count( self::$_items ) != 0 ) ) $index =
count( self::$_items );
		if( self::$_items[$index] == null ) {
			self::$_items[$index] = new MenuItem();
		}
		return self::$_items[$index];
	}
	
}




class MenuItem {
	private $_order;
	private $_title;
	private $_link;
	
	public function __construct( $order = 0 ) {
		$this->_order = $order;
	}
	
	public function Title( $var = "" ) {
		if( $var != "" ) {
			$this->_title = $var;
		}
		return $this->_title;
	}
	
	public function Link( $var = "" ) {
		if( $var != "" ) {
			$this->_link = $var;
		}
		return $this->_link;
	}
	
}



-- 
Joseph Crawford Jr.
Codebowl Solutions
codebowl at gmail.com



More information about the talk mailing list