My php function
function generateMenu($parent, $level, $menu, $utype) {
global $db;
$tree = array();
$stmt = $db->prepare("select id, parent, name FROM navigation WHERE menu=? AND user_type=?") or die($db->error);
$stmt->bind_param("ii", $menu, $utype) or die($stmt->error);
$stmt->execute() or die($stmt->error);
$stmt->store_result();
$meta = $stmt->result_metadata();
$stmt->bind_result($id, $parent, $name);
while ($stmt->fetch()) {
$arr[$id] = array(
'name' => $name,
'parent' => $parent
);
if (!array_key_exists($parent,$arr) and $parent != 0) {
$arr[$parent][$id] = $id;
}
}
$stmt->close();
}
generates following array from db table. [1], [2]
... - are ids of li
item
Array (
[1] => Array (
[name] => Parent1
[parent] => 0
)
[2] => Array (
[name] => Parent2
[parent] => 0
)
[3] => Array (
[name] => Parent3
[parent] => 0
)
[4] => Array (
[name] => Child1 of P1
[parent] => 1
)
[5] => Array (
[name] => Child2 of P1
[parent] => 1
)
)
What I want to do is to create the menu like that
<ul>
<li><a href="?page=1">Parent1</a>
<ul>
<li><a href="?page=4">Child1 of P1</a></li>
...
Second function is for generating menu from this array. But I know that before sending this array into second function I need to convert it into multidimensional tree array. I can't figure out how to do it.
Here is second function
function olLiTree($tree) {
$out = '<ul>';
foreach($tree as $key => $value) {
$out.= '<li>';
if (is_array($value)) {
$out.= $key . olLiTree($value);
} else {
$out.= $value;
}
$out.= '</li>';
}
$out.= '</ul>';
return $out;
}
Db structure