PHP功能问题 [英] PHP function issue

查看:264
本文介绍了PHP功能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试生成基于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)); 正好在之后,而
    ($ stmt-> fetch()){
    。获取我的db表的第一行为 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 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;

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功能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆