Reputation: 23290
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
die(print_r($results));
right after while
($stmt->fetch()) {
. Getting first row of my db table as Array (
[id] => 1 [parent] => 0 [name] => Sual ) 1
.Tried while ($results=$stmt->fetch()) {
instead of while ($stmt->fetch()) {
. Got following errors again
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])) {
Tried if (!array_key_exists($id, $tree[$parent]['children'])) {
instead of if (!array_key_exists($tree[$parent]['children'][$id])) {
. Got following errors again
Undefined offset: 0,1,2 on line list($id, $parent, $name) = $results;
I can't figure out what's wrong.
Upvotes: 0
Views: 279
Reputation: 100205
//try echoing the list values //Your syntax is wrong array_key_exists($yourKey, $yourSearchArray);
Upvotes: 3