【问题标题】:How to correct the following error in my PHP code to display "$title" list contains?如何更正我的 PHP 代码中的以下错误以显示 \"$title\" 列表包含?
【发布时间】:2022-08-21 03:19:08
【问题描述】:
  • 83 号线我得到并放在下面的错误来自UssdNode.php 包含变量 $item=$items[$i] 声明的文件 在下面的recurseMenu 方法中。

  • 45号线UssdTree.php 文件中的相同错误包含 变量$node=$this->treeMenu[$name]; 的声明 getNode 方法如下。

  • 94 号线UssdNode.php 文件中的相同错误包含 变量$title=$node->getTitle(); 的声明 recurseMenu 方法如下。

我的方法或函数recurseMenu(在:UssdNode.php)通常包含菜单列表(我希望展示的) 包含在变量$title=$node->getTitle() 中:

function recurseMenu($items,$bufferLimit) {
    $objectString=\"<strong>\". $this->getTitle() . \"</strong>\" . PHP_EOL;
    $lastMenu=false;
    if(count($items)>0) {
        for($i=$this->index;$i<$bufferLimit;$i++){
            $item=$items[$i];

            /* echo \"<pre>\";
            print_r($item);
            echo \"</pre>\"; */

            $num=$i+1;
            //get node by name
            $userSessions=$_SESSION[\'userSessions\'];
            $currUserSession=$userSessions[$this->address];
            $node=$currUserSession->getNode($item);
            $title=$node->getTitle();
            $objectString=$objectString . PHP_EOL .$num . \'. \' .$title;
        }
    } else {
        $objectString=$objectString.PHP_EOL . \'NO DATA AVAILABLE, TRY AGAIN LATER\';
    }
    $lastMenu=$bufferLimit==count($items);
    $objectString=$objectString . PHP_EOL . PHP_EOL . \"<strong>0. Exit</strong>\";
    if($this->getParent() != \'0\'){
        $objectString=$objectString . PHP_EOL . \"<strong>#. Back</strong>\";

    }
    if($lastMenu===false){
        $rem=count($items)-$this->index;
        $objectString=$objectString . PHP_EOL . \"<strong>99. Next (\".$rem.\")</strong>\";
    }
    return $objectString;
}

UssdTree.php 文件的getNode 方法

function getNode($name){
    $node=$this->treeMenu[$name];
    return $node;
}

当我尝试调试$this-&gt;recurseMenu($items,$bufferLimit) 在我的 toString 方法或函数中将 $title 的列表显示为字符串:

function toString(){
    $objectString=\'\';
    $items=$this->children;
    $bufferLimit=(count($items)==0)?1:$this->getBufferLimit()+1;

    echo \"<pre>\";
    print_r($this->recurseMenu($items,$bufferLimit));
    echo \"</pre>\";

    do{
        $bufferLimit-=1;
        $objectString=$this->recurseMenu($items,$bufferLimit);
    }while(strlen($objectString>160));
    $this->index=$bufferLimit;
    return $objectString;
}

问题是什么都没有显示。我收到以下错误:

注意:未定义的偏移量:3 in C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdNode.php 在第 83 行

注意:未定义索引:in C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdTree.php 在第 45 行

致命错误:未捕获的错误:调用成员函数 getTitle() on C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdNode.php:94 中的 null

堆栈跟踪:

#0 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdNode.php(59): UssdNode->recurseMenu(Array, 4)
#1 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdUserSession.php(77): UssdNode->toString()
#2 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdReceiver.php(51): UssdUserSession->fetchDisplay()
#3 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdReceiver.php(43): UssdReceiver->handleChildBearingNode(\'paybill\', \'0772247408\', \'1234567\')
#4 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdReceiver.php(15): UssdReceiver->handleContinuingRequests(\'0772247408\', \'4\', \'1234567\ ')
#5 C:\\laragon\\www\\ussd\\receiver.php(28): UssdReceiver->onMessage(Array)
#6 C:\\laragon\\www\\ussd\\receiver.php(36): MyUssdReceiver::process(Array)
#7 {main} 在第 94 行的 C:\\laragon\\www\\ussd\\ussdmenu-server-php\\UssdNode.php 中抛出

网址:http://localhost/ussd/receiver.php MSISDN:

如何在下面的recurseMenu方法中正确显示变量$title=$node-&gt;getTitle();所代表的菜单列表?

帮我解决这个错误。

  • getBufferLimit()$items 的长度有何关系?
  • 在继续前进之前,您需要解决问题。第一个错误undefined offset,没有$items[3]。在实际尝试使用它之前,您需要确保偏移量存在。这可能是一个级联修复,但唯一知道的方法是测试。 var_dump($items); 看看它实际包含什么以及有什么索引。
  • 一旦尝试访问$items[$i] 时出错,所有其他错误都会随之而来。

标签: php


【解决方案1】:

只是在访问它之前检查索引?

for ($i = $this->index; $i < $bufferLimit; $i++) {
  if (isset($items[$i])){
    $item = $items[$i];
    $num = $i + 1;
    // get node by name
    $userSessions = $_SESSION['userSessions'];
    $currUserSession = $userSessions[$this->address];
    $node = $currUserSession->getNode($item);
    $title = $node->getTitle();
    $objectString .= PHP_EOL . $num . '. ' . $title;
  }
}

或循环项目并检查是否超过缓冲区

foreach ($items as $i => $item) {
  if ($i >= $bufferLimit) {
    break;
  }
  $num = $i + 1;
  // get node by name
  $userSessions = $_SESSION['userSessions'];
  $currUserSession = $userSessions[$this->address];
  $node = $currUserSession->getNode($item);
  $title = $node->getTitle();
  $objectString .= PHP_EOL . $num . '. ' . $title;
}

您正在尝试访问不存在的密钥。如果您的 items 数组有 3 个条目并且缓冲区限制类似于 10,那么您将从数字 3 开始出现错误,因为它们不存在于数组中。

【讨论】:

  • 这似乎是一个创可贴的解决方案。正确的解决方案是修复逻辑,这样您就不会访问数组外部。
  • 要对此发表意见,我需要看到整个班级..
  • 我不。如果您按顺序遍历数组,则没有理由期望任何丢失的元素。如果有差距,那几乎肯定是设计问题。
  • 在这种情况下,我怀疑$this-&gt;getBufferLimit()+1$bufferLImit 的错误值。
【解决方案2】:

这工作正常:

foreach ($items as $i => $item) {
            if ($i >= $bufferLimit){
                break;
            }
            $num = $i + 1;
            //get node by name
            $userSessions = $_SESSION['userSessions'];
            $currUserSession = $userSessions[$this->address];
            $node = $currUserSession->getNode($item);
            $title = $node->getTitle();
            $objectString .= PHP_EOL . $num . '. ' . $title;
        }

谢谢

【讨论】:

猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 2020-07-18
  • 2023-01-23
  • 1970-01-01
  • 2015-01-16
  • 1970-01-01
  • 2022-08-24
  • 2018-02-07
相关资源
最近更新 更多