0

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

enter image description here

For testing purposes

  • Tried 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.

1 Answer 1

3
//try echoing the list values
//Your syntax is wrong
array_key_exists($yourKey, $yourSearchArray);
Sign up to request clarification or add additional context in comments.

2 Comments

please modify the code if it's possible. How will it look like?
tried print_r($results); right after list($id, $parent, $name) = $results; it echoed all rows like that Array ( [id] => 4 [parent] => 1 [name] => Yeni sual ) Yes you're right. But what I must fix? Please modify code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.