HomePHP Page 6 - Building an Extensible Menu Class
Rounding Up The Family - PHP
So you know the theory behind OOP, but don't really understandits applications? Well, it's time to take objects out of the classroom andinto the real world - this article demonstrates how OOP can save you timeand effort by building a PHP-based Menu object to describe therelationships in a hierarchical menu tree. And since the proof of thepudding is in the eating, it then combines the newly-minted Menu objectwith some of the most popular JavaScript menu systems available online toshow you how cool objects really are.
Next, one of the most useful methods in this collection - the get_children() method. This method accepts a node id and returns an array containing the next level of the menu tree.
<?
class Menu
{
// other methods
// function: get next level of menu tree
// returns: array
function get_children($id)
{
$query = "SELECT id, label, link FROM $this->table WHERE parent = '$id'";
$result = $this->query($query);
$count = 0;
while ($row = mysql_fetch_array($result))
{
$children[$count]["id"] = $row["id"];
$children[$count]["label"] = $row["label"];
$children[$count]["link"] = $row["link"];
$count++;
}
return $children;
}
}
?>
This method accepts a node id and queries the database for a
list of items which reference that node in the "parent" column. These records are packaged as an array of arrays and returned to the calling function. Here's an example of how it could be used:
<?
$obj = new Menu();
// get children
$arr = $obj->get_children(1);
echo "<ul>";
// iterate through array
for ($x=0; $x<sizeof($arr); $x++)
{
echo "<li>" . $arr[$x]["label"];
}
echo "</ul>";
?>
And this correctly displays the children of node id 1 (USA)
to be
California
Massachusetts
A useful corollary of this is the get_type() method, which can be
used to identify whether a particular node on the tree has children or not - in other words, whether it is a leaf or a branch.
<?
class Menu
{
// other methods
// function: test whether this id is a branch or leaf
// returns: boolean
function get_type($id)
{
if($this->get_children($id) )
{
return 1;
}
else
{
return 0;
}
}
}
?>
The get_ancestors() method does the reverse of the
get_children() method - it returns a list of nodes between the tree root and the supplied node identifier, starting from the top of the menu tree and proceeding downwards.
<?
class Menu
{
// other methods
// function: return a list of this node's parents
// by travelling upwards all the way to the root of the tree
// returns: array
function get_ancestors($id, $count = 0)
{
// get parent of this node
$parent = $this->get_parent($id);
// if not at the root, add to $ancestors[] array
if($parent)
{
$this->ancestors[$count]["id"] = $parent;
$this->ancestors[$count]["label"] = $this->get_label($parent);
$this->ancestors[$count]["link"] = $this->get_link($parent);
// recurse to get the parent of this parent
$this->get_ancestors($this->ancestors[$count]["id"], $count+1);
// all done? at this stage the array contains a list in bottom-up order
// reverse the array and return
return array_reverse($this->ancestors);
}
}
}
?>
Returning to the example above, an attempt to find out the
ancestors of node id 5 (Boston)
<?
$obj = new Menu();
// get children
$arr = $obj->get_ancestors(5);
echo "<ul>";
// iterate through array
for ($x=0; $x<sizeof($arr); $x++)
{
echo "<li>" . $arr[$x]["label"];
}
echo "</ul>";
?>
would return
USA
Massachusetts
Finally, the print_menu_tree() method comes in handy while debugging,
if you need to visually see the complete menu tree with its internal dependencies.
<?
class Menu
{
// other methods
// function: display complete menu tree (useful when debugging)
// returns: HTML list
function print_menu_tree($id = 0)
{
$result = $this->get_children($id);
echo "<ul>";
for ($x=0; $x<sizeof($result); $x++)
{
echo "<li>" . $result[$x]["label"] . "[" . $result[$x]["id"] . "]";
$this->print_menu_tree($result[$x]["id"]);
}
echo "</ul>";
}
}
?>