Trying to generate db driven menu which based on parent->child structure. All root menu items' parent column values are 0. Getting following errors continuously
Undefined offset: 0,1,2 on line list($id, $parent, $name) = $results;
Undefined index on line array_key_exists() expects exactly 2 parameters, 1 given on line if (!array_key_exists($tree[$parent]['children'][$id])) {
Warning: array_key_exists() expects exactly 2 parameters, 1 given on line if (!array_key_exists($tree[$parent]['children'][$id])) {
PHP CODE
<?php
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();
$bindResult = array();
while ($columnName = $meta->fetch_field()) {
$bindResult[] = &$results[$columnName->name];
}
call_user_func_array(array($stmt, 'bind_result'), $bindResult);
while ($stmt->fetch()) {
list($id, $parent, $name) = $results;
$tree[$id] = array('name' => $name, 'children' => array(), 'parent' => $parent);
if (!array_key_exists($id, $tree[$parent]['children'])) {
$tree[$parent]['children'][$id] = $id;
}
}
$stmt->close();
print_r($tree);
}
?>
And DB structure

For testing purposes
- Tried
die(print_r($results));right afterwhile ($stmt->fetch()) {. Getting first row of my db table asArray ( [id] => 1 [parent] => 0 [name] => Sual ) 1. Tried
while ($results=$stmt->fetch()) {instead ofwhile ($stmt->fetch()) {. Got following errors againUndefined index on line array_key_exists() expects exactly 2 parameters, 1 given on line if (!array_key_exists($tree[$parent]['children'][$id])) {
Warning: array_key_exists() expects exactly 2 parameters, 1 given on line if (!array_key_exists($tree[$parent]['children'][$id])) {
Tried
if (!array_key_exists($id, $tree[$parent]['children'])) {instead ofif (!array_key_exists($tree[$parent]['children'][$id])) {. Got following errors againUndefined offset: 0,1,2 on line list($id, $parent, $name) = $results;
I can't figure out what's wrong.