drawmenu($this->getmenu('menu',4)); echo $menu_html; } function drawmenu($menudata){ $html =''; foreach($menudata as $a){ extract($a); $html .= '
'; if ($active) $html.="$text"; else $html .= $text; $html .='
'; } return $html; } function getmenu($tablename, $activeid){ $parents = $this->menuparents($tablename); $chain = $this->makechain($parents,$activeid); $parentid = 0; $level = 1; $list = $this->build_hierarchy($parentid, $level, $chain, $parents); $menudata = $this->menudata($tablename, $list, $activeid); return $menudata; } function menuparents($tablename){ $parents = array(); mysql_connect('localhost','sikon','admknaz') OR die(mysql_error); mysql_select_db('smenu'); $sql = "SELECT id, parentid FROM $tablename ORDER BY sort DESC"; $result = mysql_query($sql) OR die ( "

Error at line ".__LINE__.":
" .mysql_error(). "
SQL was: $sql

" ); while ($row = mysql_fetch_assoc($result)){ $parents[$row{'id'}] = $row['parentid']; } return $parents; } function makechain($hierarchy, $activeid){ $chain = array(); if (!isset($hierarchy[$activeid])){ return array(); } $current = $activeid; do { $chain[] = $current; $current = $hierarchy[$current]; }while ($current); return $chain; } function build_hierarchy($parentid, $level, $chain, $hierarchy){ $list = array(); $brothers = array_keys($hierarchy, $parentid); foreach ($brothers as $value){ $list[$value] = $level; if (in_array($value, $chain)){ $children = $this->build_hierarchy($value, $level+1, $chain, $hierarchy); } } return $list; } function menudata($tablename, $list, $activeid){ $ids = array_keys($list); $in = implode(',', $ids); $sql = "SELECT id, text FROM $tablename WHERE id IN ($in)"; $result = mysql_query($sql) OR die( "

Error at line ".__LINE__.":
" .mysql_error(). "
SQL was: $sql

" ); $tempdata = array(); while ($row = mysql_fetch_assoc($result)){ $id = $row['id']; $level = $list[$id]; $active = ($id == $activeid) ? TRUE : FALSE; $a = $row + compact('level', 'active'); $tempdata[$row{'id'}] = $a; } foreach ($list as $key => $value){ $data[$key] = $tempdata[$key]; } return $data; } } ?>