';
if ($active) $html.= "$text";
else $html .= $text;
$html .= '';
}
return $html;
}
// пусть меню хранится в таблице mainmenu
$menutable = 'mainmenu',
$menuid = 11;
echo drawmenu(getmenu($menutable, $menuid));
function getmenu($tablename, $activeid) {
$parents = menuparents($tablename);
$chain = makechain($parents, $activeid);
$id_parent = 0; $level = 1;
$list = build_hierarchy($id_parent, $level, $chain, $parents);
$menudata = menudata($tablename, $list, $activeid);
return $menudata;
}
function menuparents($tablename) {
$parents = array();
$sql = "SELECT id, id_parent FROM project ORDER BY id_parent DESC";
$result = pg_query($link, $sql) or die("Error in query: $sSQL.
" . pg_last_error($link));
while ($row = pg_fetch_assoc($result)) {
$parents[$row{'id'}] = $row['id_parent'];
}
return $parents;
}
function makechain($hierachy, $activeid) {
$chain = array();
if (!isset($hierachy[$activeid])) {
return array();
}
$current = $activeid;
do {
$chain[] = $current;
$current = $hierachy[$current];
}
while ($current);
return $chain;
}
function build_hierarchy($id_parent, $level, $chain, $hierarchy) {
$list = array();
$brothers = array_keys($hierarchy, $id_parent);
foreach ($brothers as $value) {
$list[$value] = $level;
if (in_array($value, $chain)) {
$children = build_hierarchy($value, $level+1, $chain, $hierarchy);
$list = $list + $children;
}
}
return $list;
}
function menudata($tablename, $list, $activeid) {
$ids = array_keys($list); // вспомним, что id элементов хранятся в ключах массива
$in = implode(',', $ids);
$sql = "SELECT id, note FROM project WHERE id IN ($in)";
$result = pg_query($link, $sql) or die("Error in query: $sSQL.
" . pg_last_error($link));
$tempdata = array();
while ($row = pg_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;
}
// get the number of rows in the resultset
//$rows = pg_num_rows($result);
//echo "There are currently $rows records in the database.";
// close database connection
pg_close($link);
?>