Tural Ali
Tural Ali

Reputation: 23290

PHP function issue

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

I can't figure out what's wrong.

Upvotes: 0

Views: 279

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100205

//try echoing the list values
//Your syntax is wrong
array_key_exists($yourKey, $yourSearchArray);

Upvotes: 3

Related Questions