First let me explain my test environment so you can make any needede mofidications to the examples here.
Table
CREATE TABLE menu (
id bigint(20) NOT NULL auto_increment,
root_id bigint(20) NOT NULL default 0,
parent_id bigint(20) NOT NULL default 0,
name varchar(50) NOT NULL default '',
PRIMARY KEY (id),
UNIQUE KEY name (name),
KEY parent_id (parent_id)
) TYPE=MyISAM
Functions to populate with test data
<?php
$sql = "INSERT INTO menu (root_id,parent_id,name) VALUES (0,0,'Big Brown Fish')";
mysql_query($sql);
for($i=1;$i<50;$i++) {
$has_parent = mt_rand(0,1);
if($has_parent == 1) {
$sql = "SELECT id FROM menu ORDER BY RAND() LIMIT 0,1";
$row = mysql_fetch_assoc(mysql_query($sql));
$parent = $row['id'];
} else
$parent = 0;
$name = gen_pass();
$sql = "INSERT INTO menu (root_id,parent_id,name) VALUES (0," . $parent . ',"' . $name . '")';
mysql_query($sql);
} //end for
function gen_pass($seperator = ' ') {
$key = array();
$sizes = array('huge','large','big','average','normal','small','tiny');
$colors = array('red','orange','yellow','green','blue','indigo','violet');
$animals = array('cat','dog','fish','mouse','rat','hamster','feret','moose',
'whale','shark','termite','snake','bear','fox','goose','duck',
'rabbit','mole','deer','racoon','spider','elephant','elk','dingo');
$i1 = mt_rand(0,6);
$i2 = mt_rand(0,6);
$i3 = mt_rand(0,23);
$pswrd = ucfirst($sizes[$i1]) . $seperator . ucfirst($colors[$i2]) . $seperator . ucfirst($animals[$i3]);
return $pswrd;
}
?>
Step 1) Ensure that $GET['id'] exists so that we don't have nast notice errors showing up on the page.
<?php
if(!array_key_exists('id',$_GET))
$_GET['id'] = 0;
?>
Step 2) Build an array of all the parents that get you to the current parent.
<?php
function get_parents($id) {
$sql = "SELECT parent_id FROM menu WHERE id = $id";
$retval = array();
$row = mysql_fetch_assoc(mysql_query($sql));
if($row['parent_id'] != 0) {
foreach(get_parents($row['parent_id']) as $pid)
$retval[] = $pid;
} //end if
$retval[] = $row['parent_id'];
return $retval;
} //end get_parents
?>
Step 3) Build the menu
<?php
function build_menu($ids,$seperator = ' ',$level = 0) {
$level = intval($level);
if(!is_int($level))
return FALSE;
if(!is_array($ids))
$ids = explode(',',$ids);
$sep = '';
for($i=0;$i<$level;$i++)
$sep .= $seperator;
$retval = array();
if(ereg('^[0-9]+$',$ids[0])) {
$sql = "SELECT id,name FROM menu WHERE parent_id = " . $ids[0];
$query = mysql_query($sql);
while(($row = mysql_fetch_assoc($query)) !== FALSE) {
$retval[] = $sep . '<a href="' . $_SERVER['PHP_SELF'] . '?id=' .
$row['id'] . '">' . $row['name'] . '</a>';
if(count($ids) > 1) {
if($row['id'] == $ids[1]) {
foreach(build_menu(array_slice($ids,1),$seperator,($level + 1)) as $temp)
$retval[] = $temp;
} //end if
} elseif($row['id'] == $ids[0]) {
echo 'here';
foreach(build_menu(array($row['id']),$seperator,($level + 1)) as $temp)
$retval[] = $temp;
} //end if
} //end while
} //end if
return $retval;
} //end build_menu
?>
Okay and here is the code to call these functions and print the menu:
<?php
$ids = get_parents($_GET['id']);
$ids[] = intval($_GET['id']);
$menu = build_menu($ids);
foreach($menu as $item)
echo $item . "<br />";
?>