问题描述
尝试生成基于parent->子结构的db驱动菜单。所有根菜单项的父列值为0.连续地追踪以下错误
未定义的偏移:0,1,2 ($ id,$ parent,$ name)= $ results;
线上的未定义索引array_key_exists()期望正好2个参数,1行上给出if(!array_key_exists($ tree [$ parent] ['children'] [$ id])){
警告:array_key_exists()期望正好2个参数,1行上给出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 =?)或die($ db-> error);
$ stmt-> bind_param(ii,$ menu,$ utype)或die($ stmt-> error);
$ stmt-> execute()或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);
}
?>
和数据库结构
>
用于测试目的
- 尝试
die(print_r($ results));
正好在之后,而
。获取我的db表的第一行为
($ stmt-> fetch()){Array(
[id] => 1 [parent] => 0 [name] => Sual)1 -
尝试
while($ results = $ stmt-> fetch()){
而不是while($ stmt-> fetch()){
。再次遇到以下错误
线上的未定义索引array_key_exists()期望正好2个参数,1行上给出if(!array_key_exists($ tree [$ parent] ] [$ id])){
警告:array_key_exists()只需要2个参数,
if(!array_key_exists($ id,$ tree [$ parent] [child]] [$ id])
'child'])){而不是
if(!array_key_exists($ tree [$ parent] ['children'] [$ id])){
。再次遇到以下错误 未定义的偏移:0,1,2在线列表($ id,$ parent,$ name)= $ results;
无法弄清楚,出了什么问题。请帮助解决这个问题
//尝试回显列表值
/ /你的语法错误
array_key_exists($ yourKey,$ yourSearchArray);
Trying to generate db driven menu which based on parent->child structure. All root menu items' parent column values are 0. Geeting 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;
Can't figure out, what's wrong. Please help to fix that problem
//try echoing the list values //Your syntax is wrong array_key_exists($yourKey, $yourSearchArray);
这篇关于PHP功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!