[nycphp-talk] Re: PHP 5 Menu Objects
Joseph Crawford
codebowl at gmail.com
Wed Feb 9 10:54:38 EST 2005
Ok i am having some problems with this now.
I have tracked down where it is going wrong but i cannot see why it is doing so.
You will find all of my code at the bottom of this email.
$et->Menu()->Draw( $et->User()->Level() );
this will basically call the constructor for the Menu.class.php file.
the constructor will use a database connection to grab all menu
groups, and for each of the groups grab all the items for that group.
The issue i am having is that it seems to set the items and thier
properties, but when i retrieve them with the Draw method it shows
them as empty objects.
here is the results printed to the screen (from the Draw Method)
General
Files
Community
Control Panel
when i do a count( $items ) it returns 0 for some reason :(
General
0Files
0Community
0Control Panel
0
Here is my code.
class Menu {
static private $_groups;
public function __construct( $db ) {
$this->_groups = array();
$res = $db->Query( "SELECT id, title, req_level, display_order FROM
menu_groups" );
// make sure there are some groups in the database
if( $db->NumRows( $res ) > 0 ) {
// loop through the groups and add them to the menu
while( $group = $db->FetchArray( $res ) ) {
// add the group and set the info
$g = $this->Group( );
$g->Title( $group['title'] );
$g->Level( $group['req_level'] );
// get all items for this group
$qry = $db->Query( "SELECT id, title, req_level, display_order
FROM menu_items WHERE mgroup=".$group['id'] );
// make sure there are some menu items in this group
if( $db->NumRows( $qry ) > 0 ) {
// loop and add the items
while( $item = $db->FetchArray( $qry ) ) {
// add the item.
$i = $g->Item( );
$i->Title( $item['title'] );
$i->Link( $item['link'] );
$i->Level( $item['req_level'] );
}
}
}
}
}
static public function Group( $index = -1 ) {
if( ( $index == -1 ) && ( count( self::$_groups ) != 0 ) ) $index =
count( self::$_groups );
else $index = 0;
if( self::$_groups[$index] == null ) {
self::$_groups[$index] = new MenuGroup();
}
return self::$_groups[$index];
}
public function Draw( $level ) {
foreach( self::$_groups as $group ) {
// make sure the user has req level for this menu group
if( $level >= $group->Level() ) {
echo $group->Title().'<br>';
$items = $group->GetItems();
echo count($items);
foreach( $items as $item ) {
// make sure the user has req level for this menu item
if( $level >= $item->Level() ) {
echo ' <a
href="'.$item->Link().'">'.$item->Title().'</a><br>';
}
}
}
}
}
}
class MenuGroup {
static private $_items;
private $_order;
private $_title;
private $_level;
public function __construct( ) {
self::$_items = array();
}
public function Title( $var = "" ) {
if( $var != "" ) {
$this->_title = $var;
}
return $this->_title;
}
static public function Item( $index = -1 ) {
if( ( $index == -1 ) && ( count( self::$_items ) != 0 ) ) $index =
count( self::$_items );
else $index = 0;
if( self::$_items[$index] == null ) {
self::$_items[$index] = new MenuItem();
}
return self::$_items[$index];
}
public function GetItems() {
return self::$_items;
}
public function Level( $level = -1 ) {
if( $level != -1 ) {
$this->_level = $level;
}
return $this->_level;
}
}
class MenuItem {
private $_order;
private $_title;
private $_link;
private $_level;
public function __construct( ) {
}
public function Title( $var = "" ) {
if( $var != "" ) {
$this->_title = $var;
}
return $this->_title;
}
public function Link( $var = "" ) {
if( $var != "" ) {
$this->_link = $var;
}
return $this->_link;
}
public function Level( $level = -1 ) {
if( $level != -1 ) {
$this->_level = $level;
}
return $this->_level;
}
}
--
Joseph Crawford Jr.
Codebowl Solutions
codebowl at gmail.com
More information about the talk
mailing list