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

The function array_key_exists() needs two parameters, you only passes one. I think you mean:
while(list($id, $parent, $name) = mysql_fetch_assoc($results)) {
$tree[$id] = array(
'name' => $name,
'children' => array(),
'parent' => $parent
);
if (!array_key_exists($parent,$tree)) {
$tree[$parent]['children'][$id] = $id;
}
}
You changed your answer, so I think your problem are not with array_keys_exists. Anyway you can try with this way to obtain the data with MaxDB:
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()) {
$tree[$id] = array(
'name' => $name,
'children' => array(),
'parent' => $parent
);
if (!array_key_exists($parent,$tree)) {
$tree[$parent]['children'][$id] = $id;
}
}
$stmt->close();
print_r($tree);
}
For your second function I think @jeroen has rigth, in this anwser is what you need.
You only passed one parameter to array_key_exists(). I think you may want to use isset() to see if that element exists in the array.
array_key_exists($id, $tree[$parent]['children']);
Should be roughly equivalent to:
isset($tree[$parent]['children'][$id]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With